简体   繁体   中英

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

I have a map which has two string as key and one vector as value how can i print the value of map.

Below is my approach which is bad Can Someone help me thanks in advance

NOTE : i want to print by key not iterating on vector

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

The value of the map is a vector, assuming you can use C++11, the following code would do what you need.

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

You can print the keys by accessing the pair and then using first and second to get the first and second member of the pair respectively.

You can also print the values, by accessing the vectors and iterating over them, printing every string separately.

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

If you want to print by key not iterating on vector , then you may declare map as " std::map,string> myMap ". Then, you can do following modification to your code as given below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM