Boost logo

Boost Users :

Subject: [Boost-users] Consistency when removing items from boost multi-index using an inner iterator
From: Hovnatan Karapetyan (hovnatan_at_[hidden])
Date: 2015-11-20 10:19:09


Hi, I already posted this same question on StackOverflow, since I have not
gotten an answer yet reposting it here too.

Suppose I am iterating over a boost::multi_index using one of the two
indices. Then if I start iterating using the other index and erasing an
element according some criteria, does that invalidate the upper iterator?

E.g.,

struct person {
    string name;
    string last_name;
    string age;}
typedef multi_index<
    person,
    index_by<
        ordered_non_unique<person, string, &person::name>,
        ordered_non_unique<person, string, &person::age>
>> PersonCollection;
PersonCollection pc;
//insert some elements into pc
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(34);
    while (range1.first != range1.second) {
        if (range1.first->last_name == "Smith")
             range1.first = index1.erase(range1.first);
        else
             ++range1.first;
    }
    ++range0.first;}

So in this case, is the range0 iterator always valid? Thanks!

Hovnatan



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net