efficient loop over map

Hi

I am attempting to find an efficient loop over a map<map> data structure.

The map structure maps the following integers:
1 2 2 3 3 1 4 1 4 5 5 3 5 6 5 7 5 8 6 4 7 6 8 9 9 10

The resulting map looks as follows:
1
2
3
4
5
6
7
8
9
1| 2   
2| 3   
3| 1   
4| 1   5   
5| 3   6   7   8   
6| 4   
7| 6   
8| 9   
9| 10


Can anybody suggest how to efficiently loop over so that, given a start of say 4, the result would be
1(1), 2(2), 5(1), 3(2), 6(2), 7(2), 8(2), 9(3), 10(4)


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <map>
#include <sstream>

int digit_count(int number) {
  int digits = 0;
  if (number < 0) digits = 1; // remove this line if '-' counts as a digit
  while (number) {
      number /= 10;
      digits++;
  }
  return digits;
}

int main() {

  int v1, v2;
  std::map< int, std::map< int, int> > m;
  std::istringstream  stm {"1 2  2 3  3 1  4 1  4 5  5 3  5 6  5 7  5 8  6 4  7 6  8 9  9 10"};

  while (stm >> v1 >> v2) {
    m[v1];
    m[v1][v2] = 1;
  }

  std::cout << "Map layout " << "\n";
  std::string ss = "";
  int dc = digit_count(m.rbegin()->first); // equals max number

  for (const auto & p : m) {
    std::cout << p.first << ss.append(" ",  (dc - digit_count(p.first))) << "| ";
    for (const auto & val : p.second)
      std::cout << val.first << "   ";
    ss = "";
    std::cout << "\n";
  }
    
  int start {4};

  std::cout << "\nStart : " << start << "\n";
  std::cout << "Result : " << "\n";

  // efficient loop
  for (const auto & e : m[start])
    std::cout << e.first << " ";
    for (const auto & x : m[e.first]) // this line generates error
      std::cout << x.first << " ";

  std::cout << "\n";
  return 0;
}


I get the following error however, when the inner map key (second) is used as a key for the outer map.
1
2
3
4
5
6
g++ -g -Wall -std=c++14 -o "efficient_loop_over_map" "efficient_loop_over_map.cpp" (in directory: /c++/learn)
efficient_loop_over_map.cpp: In function ā€˜int main()ā€™:
efficient_loop_over_map.cpp:46:29: error: ā€˜eā€™ was not declared in this scope
     for (const auto & x : m[e.first])
                             ^
Compilation failed.

Any suggestions would be appreciated.


Last edited on
The reason you get an error message is because the loop on line 46-47 is not part of the loop on line 44-45. If you want a loop to contain multiple statements you need to use curly brackets (personally I always use them even when not necessary).

1
2
3
4
5
6
7
8
for (const auto & e : m[start])
{
	std::cout << e.first << " ";
	for (const auto & x : m[e.first])
	{
		std::cout << x.first << " ";
	}
}
Last edited on
Hi Peter87

thanks, can't believe I couldn't see the error.

I've modified the code as follows:
1
2
3
4
5
6
7
  // efficient loop
  for (const auto & e : m[start]) {
    std::cout << e.first << "(" << e.second << ") ";
    for (const auto & x : m[e.first])
      std::cout << x.first << "(" << (e.second + x.second) << ") ";
  }
  std::cout << "\n";


The result appears as:
 
1(1) 2(2) 5(1) 3(2) 6(2) 7(2) 8(2)


How can I modify the loop(s) so that the result comes as out?
 
1(1), 2(2), 5(1), 3(2), 6(2), 7(2), 8(2), 9(3), 10(4)


Topic archived. No new replies allowed.