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.
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
(ie the second item in the pair). See the example code in the docs .
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.