簡體   English   中英

如何閱讀地圖 <string,vector<pair<int, string> &gt;&gt;

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

我有這樣的地圖

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

但是當我嘗試讀取地圖元素時

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

我確實有錯誤

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";

錯誤 :'const類std :: vector'沒有名為'first'的成員std :: cout << it-> first <<“” << it-> second.first <<“ \\ n”;

就像Crystal一樣,它很清楚,您的地圖value可能包含許多元素,這是std::vector< std::pair<int, std::string>>

那么您將如何打印向量的元素? 選項包括:

  1. 隨機訪問(即vec[index]
  2. 迭代器(即std::vector< std::pair<int, std::string>>::const_iterator itr;
  3. 或基於范圍的for循環(即for(const auto& it: vec)

就您而言,如果您想要簡單的代碼,請使用基於范圍的循環:

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

如果您仍然想擁有較長的iterator循環,那就是它。

請在此處查看實時操作: 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;
}

您需要先訪問向量的一個元素,然后訪問其中的對。

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

更好的循環展示:

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

那么您可以有一個單獨的循環來讀取向量的內容:

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

臨時變量僅出於可讀性考慮,不需要所有副本;)

錯誤完全是編譯器告訴您的內容:

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

因此,您必須確定如何通過重載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";
    }
}

由於我們將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM