CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2016
    Posts
    9

    efficient loop over a map within a map

    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;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: efficient loop over a map within a map

    Sorry, but I'm not understanding what you are trying to achieve from the stm stream data?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured