簡體   English   中英

打印具有兩個字符串作為鍵和向量作為值的map值

[英]printing a value of map which has two string as key and vector as value

我有一個具有兩個字符串作為鍵和一個向量作為值的地圖,我該如何打印地圖的值。

以下是我的做法很糟糕,有人可以提前幫助我嗎?

注意:我想通過鍵打印而不在向量上進行迭代

int main()
{
        vector<string>value;
        std::map<std::pair<string,string> ,vector<string>> myMap;
        string input1,input2,MyvectorValue;
        for(int i=0;i<5;++i)
        {
                cin>>input1;
                cin>>input2;
                cin>>MyvectorValue;
                myMap[std::make_pair(input1,input2)].push_back(MyvectorValue);
        }
        int j=0;
        for( auto it = myMap.begin(); it != myMap.end(); ++it )
        {
                std::vector<std::string>& value = it->second.at(j++);
               cout<<value  // This is bad

           //how can i print all map value ??
        }
}

映射的值是一個矢量,假設您可以使用C ++ 11,則以下代碼將滿足您的需要。

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

int main()
{
    std::vector< std::string >value;
    std::map< std::pair<std::string , std::string> ,    std::vector<std::string> > myMap;
    std::string input1,input2,MyvectorValue;
    for(int i=0;i<5;++i)
    {

        std::cin>>input1;
        std::cin>>input2;
        std::cin>>MyvectorValue;
        myMap[std::make_pair(input1,input2)].push_back(MyvectorValue);
    }

    //If you have a particular key (string1, string2), and want to print the values for that specific key...
    auto particularKey = std::make_pair("stringA", "stringB");
    for(auto val : myMap[particularKey])
        std::cout << val << " ";
    std::cout << std::endl;

    // If you want to iterate through all keys of your map
    for(auto &elem : myMap)
    {
        std::cout << "for the pair with key (" << elem.first.first << "," << elem.first.second << "), the value is the following vector" << std::endl;
        for(auto s : elem.second)
        {
            std::cout << s << " ";
        }
        std::cout << std::endl << std::endl;
    }
    return 0;
}

您可以通過訪問密鑰對來打印密鑰,然后使用firstsecond分別獲得密鑰對的第一個和第二個成員。

您還可以通過訪問向量並對其進行迭代來打印值,分別打印每個字符串。

for(auto& element : myMap)
{
    std::cout << "Key: {" << element.first.first << ", " << element.first.second << "}\n";
    std::cout << "Value is a vector with the following strings: \n";
    for(auto& str: element.second)
        std::cout << str << std::endl;
}

如果要通過鍵打印而不在vector上進行迭代 ,則可以將map聲明為“ std :: map,string> myMap ”。 然后,您可以對代碼進行以下修改,如下所示。

int main() {
vector<string>value;
std::map<std::pair<string,string>,string> myMap;
string input1,input2,MyvectorValue;
for(int i=0; i<5; ++i) {

    cin>>input1;
    cin>>input2;
    cin>>MyvectorValue;
    myMap[std::make_pair(input1,input2)]+=MyvectorValue;
    myMap[std::make_pair(input1,input2)]+= " ";
}
for( auto it = myMap.begin(); it != myMap.end(); ++it ) {
    std::string& value = it->second;
    cout<<value<<endl;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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