gpt4 book ai didi

c++ - 如何从 map 中的集合中删除元素?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:37 25 4
gpt4 key购买 nike

我无法从 set 中删除元素在 map 内称为 accesrightsByRank . map 的键不同ACCESRIGHT小号:owner , modify , readnone . map 的值是set s 带有某些访问器的名称 ACCESRIGHT

    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 所示在 his comment ,您正在使用 const_iterator。名字本身就表明这样的迭代器不允许改变它指向的东西。更改这些行:

map<ACCESSRIGHT, set<string>>::const_iterator i;
set<string>::const_iterator j;

为此:

map<ACCESSRIGHT, set<string>>::iterator i;
set<string>::iterator j;

最快的修复。但是在此之后,请考虑问题下评论中的所有建议。

关于c++ - 如何从 map 中的集合中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32315478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com