开发者

How to iterate a C++ map of maps

开发者 https://www.devze.com 2023-04-12 12:22 出处:网络
I have a map of map std::map< int, std::map<string, double> > myMap; std::map< int, std::map<string, double> >::iterator itr;

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?


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 that myMap happens to be a member of that class? If so, what you want is to use const_iterator instead. You should do it anyways, since you are not expecting to modify the contents of the iterated elements.

0

精彩评论

暂无评论...
验证码 换一张
取 消