如何迭代 C++地图的地图
我有一个 map
std::map< int, std::map<string, double> > myMap;
std::map< int, std::map<string, double> >::iterator itr;
迭代它:
itr = myMap.find(nodeI);
if (itr == myMap.end())
{
exit(1) ;
}
导致错误:
错误:与 âoperator=â 不匹配 âitr = ((const PushList*)this)->PushList::myMap.std::map<:_Key, _Tp, _Compare, _Alloc>::find [其中 _Key = int,_Tp = std::map<:std::basic_string<:char,std::char_traits<:char>, std::allocator<:char>; >、双精度、std::less<:std::basic_string<:char、 std::char_traits<:char>, std::allocator<:char>; > >, std::allocator<:std::pair<:const std::basic_string<:char, std::char_traits<:char>、std::allocator<:char>; >,双> > >,_比较= std::less<:int>, _Alloc = std::allocator<:std::pair<:const int, std::map<:std::basic_string<:char, std::char_traits<:char>, std::allocator<:char>; >、双精度、std::less<:std::basic_string<:char、 std::char_traits<:char>, std::allocator<:char>; > >, std::allocator<:std::pair<:const std::basic_string<:char, std::char_traits<:char>、std::allocator<:char>; >,双> > > > >](((常量 int&)((const int*)((int*)nodeI))))â
如何迭代map的map?
I have a map of map
std::map< int, std::map<string, double> > myMap;
std::map< int, std::map<string, double> >::iterator itr;
Iterating it with:
itr = myMap.find(nodeI);
if (itr == myMap.end())
{
exit(1) ;
}
results in the error:
error: no match for âoperator=â in âitr = ((const PushList*)this)->PushList::myMap.std::map<:_Key, _Tp, _Compare, _Alloc>::find [with _Key = int, _Tp = std::map<:std::basic_string<:char, std::char_traits<:char>, std::allocator<:char> >, double, std::less<:std::basic_string<:char, std::char_traits<:char>, std::allocator<:char> > >, std::allocator<:std::pair<:const std::basic_string<:char, std::char_traits<:char>, std::allocator<:char> >, double> > >, _Compare = std::less<:int>, _Alloc = std::allocator<:std::pair<:const int, std::map<:std::basic_string<:char, std::char_traits<:char>, std::allocator<:char> >, double, std::less<:std::basic_string<:char, std::char_traits<:char>, std::allocator<:char> > >, std::allocator<:std::pair<:const std::basic_string<:char, std::char_traits<:char>, std::allocator<:char> >, double> > > > >](((const int&)((const int*)((int*)nodeI))))â
How can I iterate the map of map?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您发布的错误可以看出,您是在类成员 const 函数中执行此操作。
myMap
是否有可能恰好是该类的成员?如果是这样,您想要的是使用const_iterator
代替。无论如何,您都应该这样做,因为您不希望修改迭代元素的内容。From the error you posted it can be seen that you are doing this from within a class member
const
function. Is there any chance thatmyMap
happens to be a member of that class? If so, what you want is to useconst_iterator
instead. You should do it anyways, since you are not expecting to modify the contents of the iterated elements.