C++ 如何迭代此映射?
C++ 如何迭代此映射?,c++,compiler-construction,map,C++,Compiler Construction,Map,我有一个函数,其中conststd::map*pDataPool是它的输入参数之一。函数体中有一个代码段,如下所示: std::map<std::string, Array1D<unsigned short>*> DataBuffers; if (pDataPool != NULL) { for (std::map<std::string, Array2D<unsigned short>&>::iterator it = pDa
我有一个函数,其中const
std::map*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);
}
}
std::map数据缓冲区;
if(pDataPool!=NULL)
{
对于(std::map::iterator it=pDataPool->begin();
it!=pDataPool->end();it++)//错误
{
std::string sKeyName=it->first;
DataBuffers[sKeyName]=新阵列1d(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 ==========
1>e:\program files\microsoft visual studio 9.0\vc\include\map(168):错误C2529:“[]”:引用非法
1> f:\tips\tips\fy2svdataiohandler.cpp(77):请参阅对正在编译的类模板实例化“std::map”的引用
1> 与
1> [
1> _Kty=std::string,
1> _Ty=Array2D&
1> ]
1> f:\tips\tips\fy2svsdataiohandler.cpp(77):错误C2440:“初始化”:无法从“std::”树::常量迭代器转换为
“std::”树::迭代器”
1> 与
1> [
1> _Traits=std:_Tmap_Traits
1> ]
1> 没有构造函数可以采用源类型,或者构造函数重载解析不明确
1> 生成日志保存在“文件://f:\Tips\Tips\Debug\BuildLog.htm”
1> 提示-2个错误,0个警告
=========生成:0成功,1失败,0最新,0跳过===========
看这里:
您应该使用指针而不是引用。指针还有另外一个优点:它明确表示数据将被更改。看起来像是一个
pDataPool
是常量。因此,您需要使用常量迭代器:
std::map<std::string, Array2D<unsigned short>&>::const_iterator it = pDataPool->begin()
std::map::const\u迭代器it=pDataPool->begin()
用于(std::map::iterator it)
应该读
for (std::map<std::string, Array2D<unsigned short>*>::iterator it
(std::map::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>!
}
}
typedef Array3D数组;//决定1D、2D、3D、。。。!
typedef std::映射数组映射;
阵列映射数据缓冲区;
ArrayMap*数据池;
/* ... */
if(pDataPool!=NULL)
{
对于(ArrayMap::const_迭代器it=pDataPool->begin(),end=pDataPool->end();it!=end;+it)
{
const std::string&sKeyName=it->first;
DataBuffers[sKeyName]=新数组(2048);//太糟糕了,请使用共享\u ptr!
}
}
关注细节是关键。注意以下几点:
- < P>有一个原始指针作为映射类型是可怕的;如果元素已经存在,而只是用一个<代码>新的< /Cult>指针来覆盖它?内存泄漏!你应该<强>严重< /强>考虑使你的地图A为代码>代码::图 .< /P>
>P>如果有大量条目,字符串会生成一个很差的键类型。考虑<代码> STD:::无序的映射:< /代码>。(如果你在C++ 0x或MSVC10中,省略<代码>::Tr1)
在代码段中,第一行应该是:std::map DataBuffers;抱歉,我不知道如何转义SO中使用的特殊字符“”。因此,我编写了一个难看的字符串const std::map'*pDataPool。它应该是const std::map*pDataPool。有一个名为“格式化”的大型面板它就出现在你写问题的地方。读一下!原来的*数据*可能会被更改。谢谢。我访问了链接。实际上,它不能通过“for”循环。还有STL容器不能保存引用的原因:谢谢你,nimrodm。我从这个链接中理解了原因。我让服务器使用const_迭代器。但它无法解决问题。亲爱的Tomalak Geret'kal也许你是对的。我将所有参数列表从by reference std::map更改为std::map指针引用。我的程序现在通过了编译。为什么我要用std::ref来包装Array2D吗?今天你能稍微解释一下这个话题吗?再次感谢你!@goldenley:请使用“@name”语法来生成SO通知。@GoldenRef:为什么?你没有。我很困惑。你的意思是“指针”而不是“指针引用”。
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>!
}
}