[英]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.在这个 function 中,我得到了 *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>
). find
返回的迭代器是键和值的迭代器,形式为std::pair<KeyT, ValT>
(在您的例子中为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).因此,为了访问与您查找的键关联的值,您需要使用
it2->second
(即对中的第二项)。 See the example code in the docs .请参阅 文档中的示例代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.