Home > OS >  How to display a map of a map (C )?
How to display a map of a map (C )?

Time:01-23

I made the following map to store data from a .log :

map<string,pair(int,map<string,int>)>.

I manage to store the data but not to retrieve it. What I do is:

cout<< "first string: "<< debut->first 
    << " pair (first int): "  << debut->second.first << endl;

(debut is a constant iterator of a map)

With that I get the first string and the int of the pair, but I don't know how to get the content of the map. I tried different syntaxes as debut->second.second->first or debut->second.second.first but of them work.

Does someone have an idea?

CodePudding user response:

I don't know how to get the content of the map.

You can print out the content of the innermost map as shown below([DEMO]):

auto beg = debut->second.second.cbegin();
auto end = debut->second.second.cend();
while(beg!=end) //can also use for loop
{
    std::cout<<beg->first<<" ";
    std::cout<<beg->second<<std::endl;
      beg;
}

Here is a more complete example for printing the all the content of the maps, just for demonstration:

#include <iostream>
#include <map>

int main()
{
    std::map<std::string,std::pair<int,std::map<std::string,int>>> m{ { "first", { 5, {{"a", 10}, {"b", 20}} }} ,
                                                                    { "second", { 6, {{"c", 100}, {"d", 200},{"e", 300} }}},
                                                                    { "third", { 7, {{"f", 400}} } }};
    
    //////////////////////////////////////////////////////////////////
    //PRINTING THE CONTENT OF THE MAP
    
    auto debutBegin = m.cbegin(); //const iterator to beginning of outer map
    auto debutEnd = m.cend(); //const iterator to end of outer map
    
    
    while(debutBegin!=debutEnd)
    {
        auto beg = debutBegin->second.second.cbegin(); //const iterator to beginning of inner map
        auto end = debutBegin->second.second.cend();//const iterator to end of inner map
        std::cout<<debutBegin->first<<" ---- "<<debutBegin->second.first<<std::endl;
        while(beg!=end)
        {
            std::cout<<beg->first<<" ";
            std::cout<<beg->second<<std::endl;
              beg;
        }
        debutBegin  ;
    }
    
    return 0;
}

Output

The output of the above program is:

first ---- 5
a 10
b 20
second ---- 6
c 100
d 200
e 300
third ---- 7
f 400

CodePudding user response:

You probably need to iterate over the second map as well. Here's one way to do this:

map<string,pair<int,map<string,int>>> mp1;
map<string, int> mp2;

mp2["str2 inside mp1"] = 1;
mp1["str1 (key)"] = {2, mp2};


for(auto pair1 : mp1) {
    cout << "First string: " << pair1.first << "\n"; // str1 (key)
    cout << "First int: " << pair1.second.first << "\n"; // First int: 2
    
    for(auto pair2 : pair1.second.second) {
        cout << "Second string: " << pair2.first << "\n"; // Second string: str2 inside mp1
        cout << "Second int: " << pair2.second << "\n"; // Second int: 1
    }
}

However (as the comments suggest) this is a complicated approach, maybe you need to submit a new question explaining your usecase. My thought would be to seperate this into to seperate maps somehow.

CodePudding user response:

I would recommend to:

  • split your printing into functions,
  • print one map at a time, and
  • use whatever number of variables/references you need to make your code clearer.

The code below is just an example that tries to follow that intent.

[Demo]

#include <iostream>  // cout
#include <map>
#include <string>
#include <utility>  // pair

using inner_map_t = std::map<std::string, int>;
using outer_map_t = std::map<std::string, std::pair<int, inner_map_t>>;

std::ostream& operator<<(std::ostream& os, const inner_map_t& m)
{
    os << "\t{\n";
    bool first_elem{true};
    for (auto& [key, value] : m)
    {
        auto& second_string{ key };
        auto& second_int{ value };
        os
            << "\t\t" << (first_elem ? "" : ", ") << "second string: " << second_string << ", "
            << "second int: " << second_int << "\n";
        first_elem = false;
    }
    os << "\t}\n";
    return os;
}

std::ostream& operator<<(std::ostream& os, const outer_map_t& m)
{
    os << "{\n";
    bool first_elem{ true };
    for (auto& [key, value] : m)
    {
        auto& first_string{ key };
        auto& first_int{ value.first };
        auto& inner_map{ value.second };
        os
            << "\t" << (first_elem ? "" : ", ") << "first string: " << first_string << ", "
            << "first int: " << first_int << "\n"
            << inner_map;
        first_elem = false;
    }
    os << "}\n";
    return os;
}

int main()
{
    const outer_map_t m{
        { "one", { 1, {{"uno", 10}, {"cien", 100}} } },
        { "two", { 2, {{"dos", 20}} } },
        { "three", { 3, {{"tres", 30}} } }
    };

    std::cout << m << "\n";
}

// Outputs:
//
//   {
//      first string: one, first int: 1
//      {
//          second string: cien, second int: 100
//          , second string: uno, second int: 10
//      }
//      , first string: three, first int: 3
//      {
//          second string: tres, second int: 30
//      }
//      , first string: two, first int: 2
//      {
//          second string: dos, second int: 20
//      }
//   }

And another implementation, probably a bit neater.

[Demo]

#include <iostream>  // cout
#include <map>
#include <string>
#include <utility>  // pair

using inner_map_t = std::map<std::string, int>;
using pair_t = std::pair<int, inner_map_t>;
using outer_map_t = std::map<std::string, pair_t>;

template <typename T, typename U>
std::ostream& print_map(std::ostream& os, const std::map<T,U> m, size_t level_of_indent)
{
    std::string indent(4*level_of_indent, ' ');
    std::string next_indent(4*(level_of_indent   1), ' ');

    os << "{\n";
    bool first{ true };
    for (auto& [key, value] : m)
    {
        os << (first ? "" : ",\n") << next_indent << "'" << key << "' : " << value;
        first = false;
    }
    os << "\n" << indent << "}";
    return os;
}

std::ostream& operator<<(std::ostream& os, const inner_map_t& m) {
    return print_map(os, m, 1);
}
std::ostream& operator<<(std::ostream& os, const pair_t& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
std::ostream& operator<<(std::ostream& os, const outer_map_t& m) {
    return print_map(os, m, 0);
}

int main()
{
    const outer_map_t m{
        { "Alien", { 1, {{"speed", 90}, {"power", 90}} } },
        { "Jabba", { 2, {{"speed", 10}} } },
        { "T1000", { 3, {{"speed", 70}} } }
    };

    std::cout << m << "\n";
}

// Outputs:
//
//   {
//       'Alien' : (1, {
//           'power' : 90,
//           'speed' : 90
//       }),
//       'Jabba' : (2, {
//           'speed' : 10
//       }),
//       'T1000' : (3, {
//           'speed' : 70
//       })
//   }
  •  Tags:  
  • Related