如何从地图中的集合中删除元素?
How to remove an element from a set within a map?
我无法从名为 accesrightsByRank
的 map
中的 set
中删除一个元素。地图的键是不同的 ACCESRIGHT
s :owner
、modify
、read
和 none
。 map
的值是 set
s 具有某些 ACCESRIGHT
s 的访问者的名称。
map<ACCESSRIGHT, set<string>>::const_iterator i;
set<string>::const_iterator j;
for(i = this->accessrightsByRank.begin(); i != this->accessrightsByRank.end(); i++){
for(j = (*i).second.begin(); j != (*i).second.end(); j++){
if( (*j).compare(p_Username) == 0){
i->second.erase(j);
}
}
}
我以为 i->second
会给我 set
,我可以从中删除不再具有某个 ACCESRIGHT
的用户名,但似乎我做错了什么。有人可以向我解释为什么这不起作用以及我应该如何调整我的代码吗?
这是我收到的错误:
IntelliSense: no instance of overloaded function "std::set<_Kty, _Pr, _Alloc>::erase
[with _Kty=std::string
, _Pr=std::less<std::string>
, _Alloc=std::allocator<std::string>
]" matches the argument list and object (the object has type qualifiers that prevent a match)
argument types are: (std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::string>>>
)
object type is: const std::set<std::string, std::less<std::string>, std::allocator<std::string>>
如 Piotr Skotnicki in 所示,您正在使用 const_iterator
。名字本身就表明这样的迭代器不允许改变它指向的东西。更改这些行:
map<ACCESSRIGHT, set<string>>::const_iterator i;
set<string>::const_iterator j;
对此:
map<ACCESSRIGHT, set<string>>::iterator i;
set<string>::iterator j;
最快的修复。但是在此之后,请考虑问题下评论中的所有建议。
总结所有评论,
- 您正在修改地图和布景。因此你应该使用
iterator
,而不是 const_iterator
.
- 要删除集合中的某些值,您不需要线性搜索。
std::set::erase()
有一个采用要删除的值的变体。
i->second
等价于 (*i).second
. 并且更易读
this->
是多余的,除非你有一个同名的局部变量。
将它们结合起来,你会得到
map<ACCESSRIGHT, set<string>>::iterator i;
for(i = accessrightsByRank.begin(); i != accessrightsByRank.end(); i++){
i->second.erase(p_Username);
}
我无法从名为 accesrightsByRank
的 map
中的 set
中删除一个元素。地图的键是不同的 ACCESRIGHT
s :owner
、modify
、read
和 none
。 map
的值是 set
s 具有某些 ACCESRIGHT
s 的访问者的名称。
map<ACCESSRIGHT, set<string>>::const_iterator i;
set<string>::const_iterator j;
for(i = this->accessrightsByRank.begin(); i != this->accessrightsByRank.end(); i++){
for(j = (*i).second.begin(); j != (*i).second.end(); j++){
if( (*j).compare(p_Username) == 0){
i->second.erase(j);
}
}
}
我以为 i->second
会给我 set
,我可以从中删除不再具有某个 ACCESRIGHT
的用户名,但似乎我做错了什么。有人可以向我解释为什么这不起作用以及我应该如何调整我的代码吗?
这是我收到的错误:
IntelliSense: no instance of overloaded function "
std::set<_Kty, _Pr, _Alloc>::erase
[with_Kty=std::string
,_Pr=std::less<std::string>
,_Alloc=std::allocator<std::string>
]" matches the argument list and object (the object has type qualifiers that prevent a match) argument types are: (std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::string>>>
) object type is:const std::set<std::string, std::less<std::string>, std::allocator<std::string>>
如 Piotr Skotnicki in const_iterator
。名字本身就表明这样的迭代器不允许改变它指向的东西。更改这些行:
map<ACCESSRIGHT, set<string>>::const_iterator i;
set<string>::const_iterator j;
对此:
map<ACCESSRIGHT, set<string>>::iterator i;
set<string>::iterator j;
最快的修复。但是在此之后,请考虑问题下评论中的所有建议。
总结所有评论,
- 您正在修改地图和布景。因此你应该使用
iterator
,而不是const_iterator
. - 要删除集合中的某些值,您不需要线性搜索。
std::set::erase()
有一个采用要删除的值的变体。 i->second
等价于(*i).second
. 并且更易读
this->
是多余的,除非你有一个同名的局部变量。
将它们结合起来,你会得到
map<ACCESSRIGHT, set<string>>::iterator i;
for(i = accessrightsByRank.begin(); i != accessrightsByRank.end(); i++){
i->second.erase(p_Username);
}