【发布时间】:2020-07-27 16:24:44
【问题描述】:
来自 C 思维我对 c++ 标准库的经验很少,我想知道是否有人知道我在迭代 unordered_map 时如何获得指向键的指针? 更具体地说,我正在尝试做这样的事情:
std::unordered_map<std::string, int> my_map;
std::string *the_string_i_care_about;
for(auto& itr : my_map) {
if (itr.first == "pick me!" ) {
the_string_i_care_about = &itr.first;
}
}
...
do stuff with the_string_i_care_about later
如果重要的话,在我的真实代码中,我没有一对字符串和整数,而是两个 POD 结构(我将单位映射到战略游戏中的坐标)。
【问题讨论】:
-
您显示的代码有什么问题?请提供任何错误消息或您得到的错误结果。
-
&itr.first???
标签: c++ iterator unordered-map