C++ boost多索引迭代器&x27;在内环中擦除后的有效状态
这合法吗?也就是说,外部迭代器C++ boost多索引迭代器&x27;在内环中擦除后的有效状态,c++,boost,boost-multi-index,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/r
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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::multi_索引;
使用名称空间std;
结构人{
字符串名;
字符串last_name;
智力年龄;
};
typedef多索引容器<
人,
索引<
有序非唯一,
有序非唯一,
排序
>
>个人收集;
int main()
{
个人电脑;
//将一些元素插入pc
结构人kk={“约翰”,“多伊”,15};
pc.insert(kk);
kk={“约翰”,“多伊2”,17};
pc.insert(kk);
kk={“约翰”,“多伊3”,34};
pc.insert(kk);
kk={“约翰”,“史密斯”,15};
pc.insert(kk);
auto&index0=pc.get();
自动范围0=index0.equal_范围(“约翰”);
while(range0.first!=range0.second){
auto&index1=pc.get();
自动范围1=index1.相等范围(范围0.第一次->年龄);
while(range1.first!=range1.second){
如果(范围1.first->last_name==“Smith”)
range1.first=index1.erase(range1.first);
其他的
++范围1.第一;
}
++范围0.1;
}
返回0;
}
你到底想达到什么目的。你为什么要删除所有名为“史密斯”的人,这些人的年龄与名字恰好是“约翰”的人的年龄相同?:)好吧,这不是我现在面临的确切问题,但非常相似,不,这是不合法的。即使您将外部增量安排在任何擦除之前,当有多个名为“John”的人时,外部迭代器可能会失效。您到底想实现什么。你为什么要删除所有名为“史密斯”的人,这些人的年龄与名字恰好是“约翰”的人的年龄相同?:)好吧,这不是我现在面临的确切问题,但非常相似,不,这是不合法的。即使将外部增量安排在任何擦除之前,只要有多个名为“John”的人,外部迭代器也可能会失效