2011-06-14 114 views
2


我有一个函数,其中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 files\microsoft visual studio 9.0\vc\include\map(168) : error C2529: '[]' : reference to reference is illegal 
1>  f:\tips\tips\fy2svsdataiohandler.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:\tips\tips\fy2svsdataiohandler.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:\Tips\Tips\Debug\BuildLog.htm" 
1>Tips - 2 error(s), 0 warning(s) 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

在代码segement,第一行应为:的std ::地图<的std :: string,Array1D <无符号短> *>数据缓冲区; – GoldenLee 2011-06-14 07:19:16

+0

对不起,我不知道如何摆脱SO中使用的特殊字符“<" and ">”。所以,我写了一个丑陋的字符串const std :: map'<'std :: string,Array2D'''unsigned short'>'&'>'* pDataPool。它应该是const std :: map &> * pDataPool。 – GoldenLee 2011-06-14 07:24:11

+0

有一个标题为“格式”的巨大面板出现在您写问题的空间旁边。阅读! – 2011-06-14 07:30:18

回答

1

看看这里:Why are arrays of references illegal?

您应该使用指针而不是引用。指针还有其他的好处:它明确指出数据将被改变。

+0

原始*数据可能会被改变。 – 2011-06-14 07:32:04

+0

谢谢。我访问了该链接。实际上,它不能通过“for”循环。 – GoldenLee 2011-06-14 07:36:37

+0

而且为什么STL容器不能容纳引用:http://stackoverflow.com/questions/1543193/why-cant-i-store-references-in-an-stl-map-in-c – nimrodm 2011-06-14 07:37:40

2

看起来像pDataPool是恒定的。所以,你需要使用const_iterator

std::map<std::string, Array2D<unsigned short>&>::const_iterator it = pDataPool->begin() 
+0

我曾经有过使用const_iterator的情况。但它无法解决问题。 – GoldenLee 2011-06-14 07:32:43

1
for (std::map<std::string, Array2D<unsigned short>&>::iterator it 

应该读

for (std::map<std::string, Array2D<unsigned short>*>::iterator it 

你可能不存储在一个标准集装箱的引用; (你可以使用std::ref来包装它们,但那是另一天的话题......)。

+0

亲爱的Tomalak Geret'kal 也许你是对的。我通过引用std :: map &>将所有参数列表更改为std :: map *>指针引用。我的程序现在通过编译。为什么我用std :: ref包装Array2D ?你今天能对这个话题有所了解吗?再次感谢你! – GoldenLee 2011-06-14 07:52:29

+0

@GoldenLee:请使用“@name”语法生成SO通知。 – 2011-06-14 15:37:57

+0

@GoldenRef:你为什么?你没有。我很困惑。你的意思是“指针”而不是“指针参考”。 – 2011-06-14 15:38:37

0

已经有了一些答案,但让我总结一下。 (!?这是一个指针,是一维或二维)注意映射类型 - 甚至更好,使用typedef来deconfuse自己:

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。)