Hi

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

The map structure maps the following integers:
Code:
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 output from the code below appears as follows:

Code:
Map layout 
1| 2   
2| 3   
3| 1   
4| 1   5   
5| 3   6   7   8   
6| 4   
7| 6   
8| 9   
9| 10   

Start : 4
Result : 
1(1) 2(2) 5(1) 3(2) 6(2) 7(2) 8(2)
I would like to achieve a result which appears as follows instead:
Code:
1(1), 2(2), 5(1), 3(2), 6(2), 7(2), 8(2), 9(3), 10(4)

So essentially for each outer map key (from a given starting point in the map), I want to map back to the outer key, for each inner key, each jump adding 1 to a running totoal.

The loop starting at line 44 to 48, essentially does this, but only for 2 levels. The loop should end if you reach the starting point or there are no more inner map keys.

Code:
#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
w
  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 << "(" << e.second << ") ";
    for (const auto & x : m[e.first])
      std::cout << x.first << "(" << (e.second + x.second) << ") ";
  }
  std::cout << "\n";
  return 0;
}