如何显示地图的地图(C++)?

How to display a map of a map (C++)?

我制作了以下地图来存储 .log 中的数据:

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

我设法存储了数据但无法检索它。我做的是:

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

(debut是一个map的常量迭代器)

有了它,我得到了第一个字符串和对的整数,但我不知道如何获取地图的内容。我尝试了 debut->second.second->firstdebut->second.second.first 等不同的语法,但它们都有效。

有人有想法吗?

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

可以打印出最里面地图的内容如下图([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是一个比较完整的打印地图所有内容的例子,仅供演示:

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

输出

以上程序的输出为:

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

我会推荐给:

  • 将您的打印拆分为函数,
  • 一次打印一张地图,并且
  • 使用任意数量的 variables/references 让代码更清晰。

下面的代码只是一个试图遵循该意图的示例。

[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
//      }
//   }

还有另一个实现,可能更简洁一些。

[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
//       })
//   }

您可能还需要迭代第二张地图。这是执行此操作的一种方法:

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

但是(正如评论所暗示的)这是一种复杂的方法,也许您需要提交一个新问题来解释您的用例。我的想法是以某种方式将其分成单独的地图。