f**********w 发帖数: 93 | 1 我现在在用STL的map实现以下功能,读入两列数,第一列是index,第二列是相应的数据
,但是index列有重复的,需要把相应的数据累加起来,我是这么做的
map mid;
for ( int i = 0; i < MaxIndex; i++) {
map[index] += val;
}
但是我要确定读入数据前,associate data(value)是正确的初始化为0.的,这个是
map自动实现的吗?还是说我要自己实现它,谢谢 | r********g 发帖数: 1351 | | t****t 发帖数: 6806 | 3 yes, it is automatic 0 for numerical types
【在 f**********w 的大作中提到】 : 我现在在用STL的map实现以下功能,读入两列数,第一列是index,第二列是相应的数据 : ,但是index列有重复的,需要把相应的数据累加起来,我是这么做的 : map mid; : for ( int i = 0; i < MaxIndex; i++) { : map[index] += val; : } : 但是我要确定读入数据前,associate data(value)是正确的初始化为0.的,这个是 : map自动实现的吗?还是说我要自己实现它,谢谢
| r********g 发帖数: 1351 | | t****t 发帖数: 6806 | 5 小心点当然好
但是这是标准
【在 r********g 的大作中提到】 : 是标准吗?还是编译器相关? : 还是小心点好
| t****t 发帖数: 6806 | 6 23.3.1.2 map element access [lib.map.access]
T& operator[](const key_type& x);
Returns:
(*((insert(make_pair(x, T()))).first)).second.
T(), is default-initialized:
8.5 Initializers [dcl.init]
7 An object whose initializer is an empty set of parentheses, i.e., (),
shall be default-initialized.
Default-initialization means:
5 To zero-initialize storage for an object of type T means:
--if T is a scalar type (_basic.
【在 t****t 的大作中提到】 : 小心点当然好 : 但是这是标准
|
|