在内环

时间:2015-11-21 09:40:05

标签: c++ boost boost-multi-index

这合法吗?即,使用内部迭代器range0.first擦除内容使外部迭代器range1.first失效?谢谢!

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/composite_key.hpp>

#include <string>
#include <iostream>

using namespace boost::multi_index;
using namespace std;

struct person {
    string name;
    string last_name;
    int age;
};

typedef multi_index_container<
person,
    indexed_by<
    ordered_non_unique<member<person, string, &person::name> >,
    ordered_non_unique<member<person, int, &person::age> >,
    sequenced<>
    >
    > PersonCollection;


int main()
{
    PersonCollection pc;
    //insert some elements into pc
    struct person kk = {"John", "Doe", 15};
    pc.insert(kk);
    kk = {"John", "Doe2", 17};
    pc.insert(kk);
    kk = {"John", "Doe3", 34};
    pc.insert(kk);
    kk = {"John", "Smith", 15};
    pc.insert(kk);

    auto &index0 = pc.get<0>();
    auto range0 = index0.equal_range("John");
    while (range0.first != range0.second) {
        auto &index1 = pc.get<1>();
        auto range1 = index1.equal_range(range0.first->age);
        while (range1.first != range1.second) {
            if (range1.first->last_name == "Smith")
                range1.first = index1.erase(range1.first);
            else
                ++range1.first;    
        }
        ++range0.first;
    }
    return 0;
}

0 个答案:

没有答案