【发布时间】:2018-05-28 23:29:42
【问题描述】:
我不明白为什么在 std::map 中分配给迭代器->秒会修改映射中的值
#include <typeinfo>
#include <map>
#include <iostream>
int main() {
std::map<int, int> lol;
lol[2] = 33;
auto it = lol.find(2);
it->second = 91; // This changes the map's value
std::cout << typeid(it->second).name(); // This just shows 'int', not a reference
std::cout << lol[2]; // 91
return 0;
}
it->second 不只是一个 int 而不是一个引用吗?
【问题讨论】:
-
Typeid不会告诉你它是否是参考。 -
@JakeFreeman 即使使用这里的技术stackoverflow.com/questions/81870/… 也不起作用
-
@Barry 事实上,它不会。来不及修改,我就删了。
标签: c++ standard-library