问题
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