本文介绍了std :: map :: iterator返回值的副本还是值本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在地图中创建地图:
typedef map<float,mytype> inner_map;
typedef map<float,inner_map> outer_map;
我可以将内部映射放在内部映射中,还是iterator :: second返回一个副本?
Will I be able to put something inside inner map, or does iterator::second returns a copy?
stl_pair.h 表示后者:
74: _T2 second; ///< @c second is a copy of the second object
但是我的测试程序运行良好, :
but my test program run fine with the code like this:
it = my_map.lower_bound(3.1415);
(*it).second.insert(inner_map::value_type(2.71828,"Hello world!");
推荐答案
<$ c中的注释$ c> stl_pair.h 在此特定情况下具有误导性。
The comment in stl_pair.h
is misleading in this specific case.
将会有 map :: iterator
实际上是指地图中的原始资料( value_type
它本身是一个对
),它不是一个副本因此 iterator :: second
也指原始数据。
There will be no copy, since the map::iterator
actually refers to the original data inside the map (the value_type
, which itself is a pair
), it’s not a copy. Thus iterator::second
also refers to the original data.
这篇关于std :: map :: iterator返回值的副本还是值本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!