boost multi_index iterator's validity status after erasure in inner loop

久未见 提交于 2019-12-24 15:04:01

问题


Is this legal? I.e., is the outer iterator range0.first invalidated by erasing stuff using the inner iterator range1.first? Thanks!

#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;
}

来源:https://stackoverflow.com/questions/33841872/boost-multi-index-iterators-validity-status-after-erasure-in-inner-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!