简体   繁体   English

如何阅读地图 <string,vector<pair<int, string> &gt;&gt;

[英]How to read a map of <string,vector<pair<int, string>>>

I am having a map like this 我有这样的地图

typedef vector< pair<int, string> > Categories;  
map<string,Categories> cats;

but when I am trying to read elements of the map like 但是当我尝试读取地图元素时

for(map<string,Categories>::const_iterator it = cats.begin(); it != cats.end(); ++it)
 {
    std::cout << it->first << " "  << it->second.first<<"\n";
 }

I do get errors 我确实有错误

error: 'const class std::vector<std::pair<int, std::basic_string<char>
' has no member named 'first'  std::cout << it->first << " "  << it-> second.first<<"\n";

error : 'const class std::vector ' has no member named 'first' std::cout << it->first << " " << it->second.first<<"\\n"; 错误 :'const类std :: vector'没有名为'first'的成员std :: cout << it-> first <<“” << it-> second.first <<“ \\ n”;

Its clear as Crystal, that you might have many elements in your value s of your map, which is a std::vector< std::pair<int, std::string>> 就像Crystal一样,它很清楚,您的地图value可能包含许多元素,这是std::vector< std::pair<int, std::string>>

Then how would you print elements of a vector? 那么您将如何打印向量的元素? The options are: 选项包括:

  1. random access (ie, vec[index] ) 随机访问(即vec[index]
  2. iterator (ie, std::vector< std::pair<int, std::string>>::const_iterator itr; ) 迭代器(即std::vector< std::pair<int, std::string>>::const_iterator itr;
  3. or by a range based for loop (ie, for(const auto& it: vec) ) 或基于范围的for循环(即for(const auto& it: vec)

In your case, if you wanna have something simple and easy code is using a range based loop: 就您而言,如果您想要简单的代码,请使用基于范围的循环:

   for(const auto& it: cats)
   {
      std::cout << it.first << " = "; // keys
      for(const auto& ve: it.second)  // values vec
         std::cout << ve.first << "-" << ve.second << "\t";
      std::cout << std::endl;
   }

If you still wanna have long iterator loops, here is it. 如果您仍然想拥有较长的iterator循环,那就是它。

see live action here: https://www.ideone.com/3bS1kR 请在此处查看实时操作: https//www.ideone.com/3bS1kR

#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <iterator>

typedef std::vector< std::pair<int, std::string>> Categories;

int main()
{
   std::map<std::string, Categories> cats;

   cats["key1"] = {std::make_pair(1, "pair1"), std::make_pair(1, "pair2"), std::make_pair(1, "par3")};
   cats["key2"] = {std::make_pair(2, "pair1"), std::make_pair(2, "pair2")};
   cats["key3"] = {std::make_pair(3, "pair1")};

   std::cout << "Range based loop \n";
   for(const auto& it: cats)
   {
      std::cout << it.first << " = ";  // keys
      for(const auto& ve: it.second)   // values vec
         std::cout << ve.first << "-" << ve.second << "\t";
      std::cout << std::endl;
   }

   std::cout << "\nIterator loop \n";
   std::map<std::string, Categories>::const_iterator it;
   std::vector< std::pair<int, std::string>>::const_iterator curr_val_it;
   for(it = cats.cbegin(); it != cats.cend(); ++it)
   {
      std::cout << it->first << " = "; // keys

      for(curr_val_it = it->second.cbegin(); curr_val_it != it->second.cend(); ++curr_val_it )
          std::cout << curr_val_it->first << "-" << curr_val_it->second << "\t";  // values vec

      std::cout << std::endl;
   }


   return 0;
}

you need to access a element of the vector first, then the pair within. 您需要先访问向量的一个元素,然后访问其中的对。

... it->second[0].first<< ... ... it->second[0].first<< ...

better impl of loop: 更好的循环展示:

for(const auto& cat : cats)
 {
     string mapidx = cat.first;
     vector<pair<int, std::string>> catvect = cat.second;
 }

then you can have a seperate loop to read the contents of the vector: 那么您可以有一个单独的循环来读取向量的内容:

for(const auto& cat : cats)
 {
     string mapidx = cat.first;
     vector<pair<int, std::string>> catvect = cat.second;
     for (const auto& entry : catvect)
     {
         int number = entry.first;
         string whatever = entry.second;
     }
 }

the temp variables are just for readability, no need for all the copies ;) 临时变量仅出于可读性考虑,不需要所有副本;)

Error is exacly what compiler told you: 错误完全是编译器告诉您的内容:

const class std::vector ' has no member named 'first'

so, you have do decide how to print your map by overloading ostream opeartor, below example how it can be achived: 因此,您必须确定如何通过重载ostream opeartor来打印地图,以下示例如何实现:

#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

typedef std::vector<std::pair<int, std::string>> Categories;  
std::map<std::string,Categories> cats;

std::ostream& operator << (std::ostream& os, const std::vector<std::pair<int, std::string>>& v) 
{
    os << "[";
    for (auto& el : v) {
        os << " " << el.first << " : " << el.second;
    }
    os << "]";
    return os;
}

int main() {
    cats.emplace("cat1", std::vector<std::pair<int, std::string>>(1, std::make_pair(1, "category1")));
    for(auto& cat : cats) {
        std::cout << cat.first << " "  << cat.second << "\n";
    }
}

Since we are storing Vector Categories inside a Map we will have to iterate Vector too: 由于我们将Vector类别存储在Map中,因此我们也必须迭代Vector:

 for(map<string,Categories>::const_iterator it = cats.begin(); it != cats.end(); ++it)
    {
        //iterator for vector (it->second)
        for(Categories::const_iterator it2 = it->second.begin(); it2 != it->second.end(); it2++ )
        {
              std::cout << it->first << " "  << it2->first<<" "<<it2->second <<"\n";
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 地图 <string, vector <pair<int, int> &gt;&gt;推回成对? - map<string, vector <pair<int, int> > > pushing back into pair? 如何对vector &lt;pair &lt;int,pair进行排序 <int , pair<string , pair<int , int > &gt;&gt;&gt;&gt;? - How to sort vector< pair< int , pair<int , pair<string , pair<int , int > > > > >? 如何对矢量进行排序 <pair<string , pair<int , int> &gt;&gt;? - How to sort a vector<pair<string , pair<int , int>>>? 用字符串和整数对排序矢量 - Sort Vector with string and int pair 转移地图 <string, pair> 矢量 - Transfer map<string, pair> to vector 如何复制 map<string, int> 成一个向量<int, string></int,></string,> - how to copy a map <string, int> into a vector <int, string> 类型为typedef映射的数据成员的类中的编译器错误 <std::string,std::pair<std::string,vector<int> &gt;&gt; MapPairList ;? - compiler error in class with datamember of type typedef map<std::string,std::pair<std::string,vector<int>>> MapPairList;? 如何转换矢量<pair<string, t> &gt; 到矢量<pair<string,string> &gt; </pair<string,string></pair<string,> - How to convert vector<pair<string, T>> to vector<pair<string,string>> 如何复制向量中的字符串<pair<string, int> &gt; 到矢量<string> ? </string></pair<string,> - How do I copy the strings in a vector<pair<string, int>> to vector<string>? 如何从地图中迭代和查找<pair<string, int> , 一对<string, Array> &gt;? - How to iterate and find from map<pair<string, int>, pair<string, Array>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM