2

I am trying to do the below sample in unordered map in c++

my_dict = {'one': ['alpha','gamma'], 'two': ['beta'], 'three' : ['charlie']}
print(my_dict["one"]) // ['alpha','gamma']

I tried using find operator like below

int main ()
{
std::unordered_map<std::string, std::vector<std::string>> dict;
dict["one"].push_back("alpha");
dict["one"].push_back("beta");
dict["two"].push_back("gamma");
 auto it = dict.find("one");
 cout<<it->second<<endl; // expected output alphabeta
return 0;
}

But I am not able to retrieve the value for this key dict["one"]. Am i missing anything ? Any help is highly appreciated. Thanks

learner
  • 117
  • 2
  • 10
  • 3
    `it->second` is a `std::vector` object, and `std::vector` objects cannot be printed to `std::cout` directly (it does not have an `operator<<` for `std::ostream`s). If you want to print the vector, you need to actually print the elements individually – Human-Compiler Feb 04 '21 at 04:26
  • 1
    Relevant: [How do I print out the contents of a vector?](https://stackoverflow.com/q/10750057/580083) – Daniel Langr Feb 04 '21 at 04:27
  • @Human-Compiler thanks for responding. how to print individually? you mean like this `for (auto i: it) std::cout << i << ' ';` ? – learner Feb 04 '21 at 04:39
  • @learner it should be `for (auto i: it->second) std::cout << i << ' ';` if you use rb-for. That's indeed a correct way to print the vector. – con ko Feb 04 '21 at 05:24

2 Answers2

2

This is because your it->first will point to the key of the dictionary i.e. "One" and it->second will point to the value i.e. the vector.

So to print elements of the vector you need to specify the indexes of the vector that you are printing as well. The following code will give you the result you want:

int main() {
std::unordered_map <std::string, std::vector<std::string>> dict;
dict["one"].push_back("alpha");
dict["one"].push_back("beta");
dict["two"].push_back("gamma");
auto it = dict.find("one");
cout<<it->second[0]<<it->second[1]<<endl; // expected output alphabeta
return 0;

}

P.S. Please accept my answer if you find it useful as that would help me get some reputation points

Unyaya
  • 68
  • 7
  • thanks for responding. i am accepting @Human-Compiler answer because the answer is more efficient because using for loop to get all the elements in the vector. I can upvote your answer, but StackOverflow is not allowing me because of my reputation.. – learner Feb 05 '21 at 01:54
2

The failure you are encountering is due to it->second being a std::vector object, which cannot be printed to std::cout because it lacks an overload for operator<<(std::ostream&,...).

Unlike languages like Python that do this for you, in C++ you must manually loop through the elements and print each entry.

To fix this, you will need to change this line:

 cout<<it->second<<endl; // expected output alphabeta

To instead print each object in the container. This could be something simple like looping through all elements and printing them:

for (const auto& v : it->second) {
    std::cout << v << ' '; // Note: this will leave an extra space at the end
}
std::cout << std::endl;

Or you can go more complex if the exact formatting is important.


@DanielLangr posted a link in the comments to the question that summarizes all possible ways of doing this, and I recommend taking a look if you're wanting anything more complex: How do I print the contents to a Vector?

Human-Compiler
  • 11,022
  • 1
  • 32
  • 59