jeudi 27 septembre 2018

Efficient way to get key from value when map contain vector of string as value

How to get key using value which is vector of string and vice versa. Below is my code.

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

using namespace std;
int main()
{
std::unordered_map<std::string, std::vector<std::string>> Mymap;
Mymap["unique1"] = {"hello", "world"};
Mymap["unique2"] = {"goodbye", "goodmorning", "world"};
Mymap["unique3"] = {"sun", "mon", "tue"};

for(auto && pair : Mymap) {
        for(auto && value : pair.second) {
                std::cout << pair.first<<" " << value<<"\n";
                if(value == "goodmorning") // how get key i.e unique2 ?
        }}
}

case 1: When value is input. key is output.

Input  : goodmorning
output : unique2

case 2: When key is input value is output.

Input : unique3
output: sun ,mon ,tue

Note : No boost library available.

Aucun commentaire:

Enregistrer un commentaire