【发布时间】:2014-06-29 07:20:48
【问题描述】:
对由 STD 集合返回的元素(例如地图)的引用在多长时间内有效?
例如,在这段代码中:
struct Employee{
int salary;
string name; // the key
};
map<string,Employee> allemployees;
...
Employee & Joe = allemployees["Joe Smith"];
Joe.salary=150; // change "Joe Smith"'s salary
assert(allemployees["Joe Smith"].salary==150); //always true
....
allemployees["Mark Jones"]= Employee();
... // No "Joe Smith" operations in the dots
Joe.salary=200;
assert (allemployees["Joe Smith"].salary==200); //true or not?
}
换句话说,我从地图中得到一个值引用。但随后我在底层地图上进行了各种其他插入、删除等操作。原值参考还好吗?其他收藏呢?
还有,我怎么知道的?我查看了 Stroustrup,但什么也没看到。
来自 C 背景,我对引用和集合及其交互感到困惑。我是否应该考虑其值本身就是引用的地图?这意味着什么?
所以一个更一般的问题:我在哪里可以找到这个问题和类似问题的规范答案?
[这是一个已删除问题的修订版]
【问题讨论】:
-
STD 在大写时是一个令人困惑的首字母缩略词
;-) -
@rubenvb - 是的...... “性病收藏中的一生记忆”