STL 2-D map iterator question

Hi,
I'm trying to iterate through a 2-dimensional (nested) map.

I think I'm following syntax correctly, but keep getting compilation errors.

System:
Linux dracula 2.6.32-30-server #59-Ubuntu SMP Tue Mar 1 22:46:09 UTC 2011 x86_64 GNU/Linux

$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

Error message and code follow.

wordPairs is defined as:
map <string, map <string, float> > wordPairs;

thanks for help!
Nelag

-------------------------------------------
Error message:
TextParser.cpp: In member function ‘void TextParser::findDegreeDistribution()’:
TextParser.cpp:358: error: no match for ‘operator=’ in ‘currentWordA = ((TextParser*)this)->TextParser::wordPairs.std::map<_Key, _Tp, _Compare, _Alloc>::begin [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, 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> >, float> > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, 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> >, float> > > > >]()’
/usr/include/c++/4.4/bits/stl_tree.h:154: note: candidates are: std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, 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> > > > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, 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> > > > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, 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> > > > >&)
-----------------------------------


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void TextParser::findDegreeDistribution() {

  int index = 0;
  int maxDegree = 0;

  map <string, map <string, int> >::iterator currentWordA;
  map <string, int>::iterator currentWordB;

  for (currentWordA = wordPairs.begin();
       currentWordA != wordPairs.end();
       currentWordA++)    {

    for (currentWordB = wordPairs[*currentWordA].begin();
         currentWordB != wordPairs.end();
         currentWordB++)    {

      if (wordPairs[*currentWordA][*currentWordB] > 0)
        index++;

    }

    if (index != 0) {
      if (index > maxDegree)
        maxDegree = index;

      index = 0;
    }

  }

}
Last edited on
Remember to use [code][/code] tags.

You don't use iterators like that. An iterator points to the element itself (for maps, internally it is an std::pair):

1
2
3
map <string, map <string, int> >::iterator iter = the_map.begin();
iter->first; //this is the "string" part
iter->second; //this is the map<string, int> part 
Thanks, but I should have mentioned that the above error is coming from line 9.
You said that your map of maps was defined with a float:
 
map <string, map <string, float> > wordPairs;

But your iterator is defined with an int:
 
map <string, map <string, int> >::iterator currentWordA;

Ack, thank you Galik and firedraco, that fixed it! Really great to get this working - a useful data structure.
Topic archived. No new replies allowed.