Mehmet B.
Mehmet B.

Reputation: 17

std::unordered_map iterator deference problem

I have a code as below:

#include <iostream>
#include <unordered_map>
#include <string> 

void foo(const std::unordered_map<char, std::string>& uom){

    auto it2=uom.find('S');

    if(it2!=uom.end())  //NEVER FORGET THIS
        std::cout<< *it2 <<std::endl;
}

In this function I get error for *it2. The error is "invalid operands to binary expression." I couldn't find to solve this error. Can anyone help me? Thank you.

Upvotes: 1

Views: 50

Answers (1)

Alex
Alex

Reputation: 632

The iterator returned by find is an iterator over keys and values, in the form of a std::pair<KeyT, ValT> (in your case std::pair<char, std::string>). So in order to access the value associated with the key you've looked up, you need to use it2->second (i.e. the second item in the pair). See the example code in the docs.

Upvotes: 2

Related Questions