user1593881
user1593881

Reputation:

Dereference the first part of the pair object for an unordered map

I have a C++ 03 compliant compiler. I am using a pair object when inserting the element into an unordered map using the following code:

unordered_map<char, string> mymap;
pair<unordered_map<char, string>::iterator,bool> ret;
ret = mymap.insert(make_pair('A',"A string of some sort."));
    if (ret.second==false)
        cout << "Could not insert an element into an unordered map.\n";
    else
        cout << // how to print the ret.first value here

I am having trouble accessing the value pointed to by ret.first. How to print out , dereference iterator that is ret.first?

Upvotes: 0

Views: 1482

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

You could do something like this

cout << (ret.first)->first;

Output

A

Working demo

When you say ret.first you are accessing the iterator from the returned std::pair<std::unordered_map<char, std::string>::iterator, bool>.

Once you have the iterator, the next ->first gets at the char, which serves as the key. Similarly ->second would get at the std::string value for that key.

Upvotes: 3

Related Questions