How can I display the content of a map on the console?

前端 未结 7 1492
轻奢々
轻奢々 2020-12-28 13:41

I have a map declared as follows:

map < string , list < string > > mapex ; list< string > li;

How can I disp

7条回答
  •  一整个雨季
    2020-12-28 13:47

    I'm a little off topic here...

    I guess you want to dump the map content for debugging. I like to mention that the next gdb release (version 7.0) will have a built in python interpreter which will be used by the gcc libstdc++ to provide stl pretty printers. Here is an example for your case

      #include 
      #include 
      #include 
      #include 
    
      using namespace std;
    
      int main()
      {
        typedef map > map_type;
        map_type mymap;
    
        list mylist;
        mylist.push_back("item 1");
        mylist.push_back("item 2");
        mymap["foo"] =  mylist;
        mymap["bar"] =  mylist;
    
        return 0; // stopped here
      }
    

    which results in

    (gdb) print mymap
    $1 = std::map with 2 elements = {
      ["bar"] = std::list = {
        [0] = "item 1",
        [1] = "item 2"
      },
      ["foo"] = std::list = {
        [0] = "item 1",
        [1] = "item 2"
      }
    }
    

    Yay!

提交回复
热议问题