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

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

本文关键字:地图 C++ 显示 何显示      更新时间:2024-09-23

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

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

我设法存储数据,但不检索它。我所做的是:

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

(首秀是地图的常量迭代器(

这样我就得到了这对的第一个字符串和int,但我不知道如何得到映射的内容。我尝试了不同的语法,如debut->second.second->firstdebut->second.second.first,但它们都有效。

有人有主意吗?

我不知道如何获取地图的内容。

您可以打印出最里面地图的内容,如下所示([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;
}

这里有一个更完整的例子来打印地图的所有内容,只是为了演示:

#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

您可能还需要迭代第二个映射。这里有一种方法:

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

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

我建议:

  • 将打印拆分为多个功能
  • 一次打印一张地图,以及
  • 使用任意数量的变量/引用,使代码更加清晰

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

【演示】

#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
<< "tt" << (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
//      }
//   }

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

【演示】

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