如何迭代此映射

How to iterate this map?

本文关键字:映射 迭代 何迭代      更新时间:2023-10-16


我有一个函数,其中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。)