如何迭代此映射
How to iterate this map?
我有一个函数,其中const std::map<std::string, Array2D<unsigned short>&>* pDataPool
是它的一个输入参数。函数体中有一个代码片段如下:
std::map<std::string, Array1D<unsigned short>*> DataBuffers;
if (pDataPool != NULL)
{
for (std::map<std::string, Array2D<unsigned short>&>::iterator it = pDataPool->begin();
it != pDataPool->end(); it++) // Error
{
std::string sKeyName = it->first;
DataBuffers[sKeyName] = new Array1D<unsigned short>(2048);
}
}
编译器输出:
1>e:program filesmicrosoft visual studio 9.0vcincludemap(168) : error C2529: '[]' : reference to reference is illegal
1> f:tipstipsfy2svsdataiohandler.cpp(77) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=Array2D<unsigned short> &
1> ]
1>f:tipstipsfy2svsdataiohandler.cpp(77) : error C2440: 'initializing' : cannot convert from 'std::_Tree<_Traits>::const_iterator to <br/>'std::_Tree<_Traits>::iterator'
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,Array2D<unsigned short> &,std::less<std::string>,std::allocator<std::pair<const <br/> std::string,Array2D<unsigned short> &>>,false>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>Build log was saved at "file://f:TipsTipsDebugBuildLog.htm"
1>Tips - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
看起来pDataPool
是常量。所以你需要使用const_iterator
:
std::map<std::string, Array2D<unsigned short>&>::const_iterator it = pDataPool->begin()
看看这里:为什么引用数组是非法的?
应该使用指针而不是引用。指针还有另外的优点:它明确地指示数据将被更改。
for (std::map<std::string, Array2D<unsigned short>&>::iterator it
应读取
for (std::map<std::string, Array2D<unsigned short>*>::iterator it
您不能将引用存储在标准容器中;(您可以使用std::ref
包装它们,但这是另一天的主题…).
已经有了一些答案,但让我总结一下。注意映射的类型(这是一个指针!它是1D还是2D?)——或者更好的是,使用typedefs来解除融合:
typedef Array3D<unsigned short> Array; // decide on 1D, 2D, 3D, ...!
typedef std::map<std::string, Array*> ArrayMap;
ArrayMap DataBuffers;
ArrayMap * pDataPool;
/* ... */
if (pDataPool != NULL)
{
for (ArrayMap::const_iterator it = pDataPool->begin(), end = pDataPool->end(); it != end; ++it)
{
const std::string & sKeyName = it->first;
DataBuffers[sKeyName] = new Array(2048); // terrible, use shared_ptr<Array>!
}
}
注重细节是关键。几个注意事项:
将原始指针作为映射类型是很糟糕的;如果元素已经存在,而您只是用
new
指针覆盖它,该怎么办?内存泄漏!您应该认真地考虑将地图设为std::map<std::string, std::tr1::shared_ptr<Array> >
。如果有很多条目,字符串会导致键类型不好。请考虑
std::tr1::unordered_map
。(如果您在C++0x或MSVC10中,请省略::tr1
。)
相关文章:
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 映射迭代器与运算符不匹配
- 我可以将映射迭代器与 OpenMP 并行使用吗?
- 取消引用映射迭代器时返回对临时的引用
- 为什么映射迭代器显示基本操作数无效错误
- 复制映射迭代器对值的省略
- ->第一个/第二个到空映射迭代器开始
- 由引用集/映射迭代器传递的迭代器不可取消引用
- 在 GCC 上自动声明映射迭代器错误
- 指向同一项的两个映射迭代器是否保证相等
- 嵌套映射迭代器
- 模板标准::映射::迭代器实例化
- 映射迭代器遍历技术
- 映射迭代器与手动
- VC++11映射和多映射迭代器(重载)C2535
- 切换情况下映射迭代器的交叉初始化
- C++-映射迭代器中的if语句导致程序崩溃的原因
- 映射迭代器:一元'*'的无效类型参数(具有"int")
- 比较未引用的映射迭代器(std::pairs):C2678
- 与C++中擦除映射迭代器的行为不一致