【问题标题】:How to use vector of map如何使用地图矢量
【发布时间】:2016-01-12 07:04:01
【问题描述】:
    vector <map<string,string>> dictionary;
    map <string, string> word1;
    map <string, string> word2;

    word1.insert(pair<string, string>("UNREAL","Abc"));
    word2.insert(pair<string, string>("PROPS","Efg"));

    dictionary.push_back(word1);
    dictionary.push_back(word2);

    vector<map<string, string>>::iterator it;
    it = dictionary.begin();

    for( it; it != dictionary.end(); it++)
    {
                cout << it << " " << it << endl; //ERROR
    }

我想显示存储在向量中的数据。请建议我如何显示矢量字典的输出?

【问题讨论】:

    标签: c++ dictionary vector


    【解决方案1】:

    为了解决您的问题,您应该这样做:

    for(it = dictionary.begin(); it != dictionary.end(); it++){
        for(auto it1=it->begin();it1!=it->end();++it1){
            cout << it1->first << " " << it->second << endl; 
        }
    }
    

    但是,我认为设计有问题。在您的情况下,您不需要vector 的maps ...您需要vector 的pairs 或只需要map。

    向量对:

    std::vector <std::pair<string,string>> dictionary;
    dictionary.emplace_back("UNREAL","Abc");
    dictionary.emplace_back("PROPS","Efg");
    for(auto const& item:dictionary){
        std::cout << item.first << " " << item.second;
    }
    

    地图:

     std::map<string,string> dictionary;
     dictionary.insert("UNREAL","Abc");//also :  dictionary["UNREAL"]="Abc";
     dictionary.insert("PROPS","Efg");//also :  dictionary["PROPS"]="Efg";
     for(auto const& item:dictionary){
         std::cout << item.first << " " << item.second;
     }
    

    因为地图不仅仅是一对东西,它是一组对(有点不准确)。

    【讨论】:

    • 我是编程初学者。我不知道单个地图可以存储多个数据。感谢您的解释。
    • 欢迎您,如果您的问题已解决,请不要忘记在绿色标记处打勾
    【解决方案2】:
    // i is each map in your vector
    for (auto i : dictionary) {
        // j is each std::pair<string,string> in each map
        for (auto j : i) {
          // these are the two strings in each pair
          j.first; j.second;
      } 
    }
    

    这个答案需要 c++11,但现在几乎所有东西都支持它。

    【讨论】:

      【解决方案3】:

      vector 和 map 是两个容器,所以你需要两个嵌套循环。在地图上的嵌套循环中,您的迭代器引用std::pairs,因此您使用 .first 访问键,使用 .second 访问值:

      vector <map<string,string> > dictionary;
      map <string, string> word1;
      map <string, string> word2;
      
      word1.insert(pair<string, string>("UNREAL","Abc"));
      word2.insert(pair<string, string>("PROPS","Efg"));
      
      dictionary.push_back(word1);
      dictionary.push_back(word2);
      
      vector<map<string, string> >::iterator it;
      for( it = dictionary.begin(); it != dictionary.end(); ++it)
      {
          map<string, string>::iterator it;
          for( nested = it.begin(); nested != it.end(); ++nested)
          {
              cout << it->first << " " << it->second << endl; //ERROR
          }
      }
      

      如果你使用 C++11,你可以使用 Range-based for-loops 来缩短它:

      vector <map<string,string>> dictionary;
      map <string, string> word1;
      map <string, string> word2;
      
      word1.insert(pair<string, string>("UNREAL","Abc"));
      word2.insert(pair<string, string>("PROPS","Efg"));
      
      dictionary.push_back(word1);
      dictionary.push_back(word2);
      
      for(const map<string, string> &outer : dictionary)
      {
          for(const pair<string, string> & inner : outer)
          {
              cout << inner.first << " " << inner.second << endl; //ERROR
          }
      }
      

      【讨论】:

      • 使用auto &amp; 或const auto &amp; 代替const map&lt;string, string&gt; 和const pair&lt;string, string&gt;。
      • //这也有效。 for(it = dictionary.begin(); it != dictionary.end(); it++) { cout begin()->first begin()->second
      • @JohanLundberg 我认为在这种情况下,嵌套容器编写实际类型可以让没有太多 C++11 经验的人更容易了解实际情况。
      【解决方案4】:

      有类似问题here的答案。该代码将允许您使用 &lt;&lt; 运算符打印带有 any 模板参数的 any stl 容器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-02
        相关资源
        最近更新 更多