C++ 如何在控制台上显示地图的内容?

C++ 如何在控制台上显示地图的内容?,c++,dictionary,stl,stdmap,C++,Dictionary,Stl,Stdmap,我有一张地图,声明如下: map < string , list < string > > mapex ; list< string > li; std::map<std::string, std::list<std::string>> mapex; mapex["a"] = { "1", "2", "3", "4" }; mapex["b"] = { "5", "6", "7" }; for (const auto &[k

我有一张
地图
,声明如下:

map < string , list < string > > mapex ; list< string > li;
std::map<std::string, std::list<std::string>> mapex;
mapex["a"] = { "1", "2", "3", "4" };
mapex["b"] = { "5", "6", "7" };

for (const auto &[k, v] : mapex) {
    std::cout << "m[" << k.c_str() << "] =";
    for (const auto &s : v)
        std::cout << " " << s.c_str();
    std::cout << std::endl;
}
map>mapex;列表li;
如何在控制台上显示上述地图中存储的项目?

我将尝试以下方法

void dump_list(const std::list<string>& l) {
  for ( std::list<string>::const_iterator it = l.begin(); l != l.end(); l++ ) {
    cout << *l << endl;
  }
}

void dump_map(const std::map<string, std::list<string>>& map) {
  for ( std::map<string,std::list<string>>::const_iterator it = map.begin(); it != map.end(); it++) {
    cout << "Key: " << it->first << endl;
    cout << "Values" << endl;
    dump_list(it->second);
}
void dump_列表(const std::list&l){
对于(std::list::const_迭代器it=l.begin();l!=l.end();l++){

cout这取决于您希望如何显示它们,但您可以随时轻松地迭代它们:

typedef map<string, list<string>>::const_iterator MapIterator;
for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++)
{
    cout << "Key: " << iter->first << endl << "Values:" << endl;
    typedef list<string>::const_iterator ListIterator;
    for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++)
        cout << " " << *list_iter << endl;
}
typedef映射::常量迭代器映射迭代器;
对于(MapIterator iter=mapex.begin();iter!=mapex.end();iter++)
{

我有点离题了

我猜您希望转储映射内容以进行调试。我想提到的是,下一个gdb版本(7.0版)将有一个内置的python解释器,gcc libstdc++将使用该解释器来提供stl漂亮的打印机。下面是一个示例

  #include <map>
  #include <map>
  #include <list>
  #include <string>

  using namespace std;

  int main()
  {
    typedef map<string, list<string> > map_type;
    map_type mymap;

    list<string> mylist;
    mylist.push_back("item 1");
    mylist.push_back("item 2");
    mymap["foo"] =  mylist;
    mymap["bar"] =  mylist;

    return 0; // stopped here
  }

耶!

另一种形式,使用

void打印对(const-pair&p)
{
cout更新(回到未来):使用基于C++11范围的for循环-

std::map<Key, Value> m { ... /* initialize it */ ... };

for (const auto &p : m) {
    std::cout << "m[" << p.first << "] = " << p.second << '\n';
}
std::map m{…/*初始化它*/…};
用于(常数自动和p:m){

std::cout您可以编写一个非常通用的重载函数,它有两个用途:

  • 它适用于任何
    地图
  • 它允许使用
    如果您可以使用特性,那么我认为如中所建议的那样,提供最可读的选项。但是,如果您可以使用,那么您可以将这些循环与组合起来以进一步提高可读性,因为您不再需要使用
    第一个
    第二个
    成员y解决方案如下所示:

    map < string , list < string > > mapex ; list< string > li;
    
    std::map<std::string, std::list<std::string>> mapex;
    mapex["a"] = { "1", "2", "3", "4" };
    mapex["b"] = { "5", "6", "7" };
    
    for (const auto &[k, v] : mapex) {
        std::cout << "m[" << k.c_str() << "] =";
        for (const auto &s : v)
            std::cout << " " << s.c_str();
        std::cout << std::endl;
    }
    
    std::map mapex;
    mapex[“a”]={“1”、“2”、“3”、“4”};
    mapex[“b”]={“5”、“6”、“7”};
    对于(常数自动和[k,v]:mapex){
    
    std::cout get
    在dump_列表中的“之前”应具有非限定id,它应该类似于:std::vector::const_迭代器it=l.begin();for(it;it!=l.end();++it){std::cout@triclosan:你也可以预先递增,在这种情况下没有关系。或者我误解了你吗?将递增后的增益用于额外的临时对象creation@triclosan:也许,或者编译器会为您优化它。如果不尝试,这是不可能的。在总体方案中,我们使用的是标准的iostream con唯一的输出,在性能方面使额外的对象迭代器对象的成本相形见绌。
    
    template<class key_t, class value_t>
    ostream& operator<<(ostream& os, const map<key_t, value_t>& m) {
        for (typename map<key_t, value_t>::const_iterator it = m.begin();
                it != m.end(); it++) {
            os << "Key: " << it->first << ", Value: " << it->second;
        }
        return os;
    }
    
    template<class T>
    ostream& operator<<(ostream& os, const list<T>& l) {
        for (typename list<T>::const_iterator it = l.begin(); it != l.end(); it++) {
            os << "\"" << *it << "\", ";
        }
        return os;
    }
    
    std::map<std::string, std::list<std::string>> mapex;
    mapex["a"] = { "1", "2", "3", "4" };
    mapex["b"] = { "5", "6", "7" };
    
    for (const auto &[k, v] : mapex) {
        std::cout << "m[" << k.c_str() << "] =";
        for (const auto &s : v)
            std::cout << " " << s.c_str();
        std::cout << std::endl;
    }