C++ 无序映射迭代器差异问题
我有一个代码如下:C++ 无序映射迭代器差异问题,c++,C++,我有一个代码如下: #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::
#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;
}
#包括
#包括
#包括
无效foo(常数标准::无序图和计量单位){
自动it2=计量单位查找('S');
if(it2!=uom.end())//永远不要忘记这一点
std::cout由find
返回的迭代器是对键和值的迭代器,其形式为std::pair
(在您的例子中是std::pair
)。因此,为了访问与您查找的键相关联的值,您需要使用it2->second
(即该对中的第二项)请参阅./p>中的示例代码。检查返回类型的文档<代码> STD::unOrdEdSMAP::FAND()/<代码>。具体地是迭代器类型。它不是你所想的。你得到的答案是解决问题的吗?如果是,请考虑它。