我有一个 2 x 3 set<set<int> >
名为 ss
像这样:
5 6 7
6 7 8
我想删除所有 6
在里面,结果是这样的:
5 7
7 8
我正在尝试:
for (set<set<int> >::iterator it = ss.begin(); it != ss.end(); it++) {
it->erase(6);
}
这给了我一个错误:
error: passing ‘const std::set<int>’ as ‘this’ argument of ‘std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = int, _Compare = std::less<int>, _Alloc = std::allocator<int>, std::set<_Key, _Compare, _Alloc>::size_type = long unsigned int, std::set<_Key, _Compare, _Alloc>::key_type = int]’ discards qualifiers [-fpermissive]
我可以通过传递 -fpermissive
来编译它它似乎工作正常,但我想知道这个错误是怎么回事。
EDIT 在我尝试了海德的建议后:
for (set<set<int> >::iterator it = ss.begin(); it != ss.end(); it++) {
set<int> temp(*it);
temp.erase(6);
ss.erase(*it);
ss.insert(temp);
}
这似乎有效,所以我猜集合不允许像他说的那样更改元素..
最佳答案
您正在尝试更改不允许的 key
关于c++ - 从 C++ 中的二维集合中删除一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12997789/