jus*_*tin 8 c++ string stl map name-decoration
这是我第一次尝试C++ STL.我正在尝试使用map构建一个多维关联数组.例如:
typedef struct DA {
string read_mode;
string data_type;
void *pValue;
void *pVarMemLoc;
}DA;
int main()
{
map<string, map<string, map<string, map<string, map<string, DA*>>>>> DATA;
DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"] = new DA;
DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"] = new DA;
DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"] = new DA;
IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"]->read_mode = "file";
IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"]->read_mode = "poll";
IEC["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"]->read_mode = "report";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在VS2005中编译上面的代码时,我收到了170个C4503警告.所有警告都是关于"超出装饰名称长度,名称被截断".该程序似乎运行正常.
任何人都要花些时间向我解释是什么原因引起了这些警告以及如何解决这些问题?提前致谢 :)
Warning 1 warning C4503: 'std::map<_Kty,_Ty>::~map' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 2 warning C4503: 'std::map<_Kty,_Ty>::map' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 3 warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 4 warning C4503: 'std::_Tree<_Traits>::~_Tree' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 5 warning C4503: 'std::map<_Kty,_Ty>::operator []' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 6 warning C4503: 'std::_Tree<_Traits>::iterator::~iterator' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Warning 7 warning C4503: 'std::_Tree<_Traits>::iterator::iterator' : decorated name length exceeded, name was truncated c:\program files\microsoft visual studio 8\vc\atlmfc\include\cstringt.h 2121
Run Code Online (Sandbox Code Playgroud)
我不是禁用警告的粉丝,因为根据我的研究,这个警告可能会产生意想不到的后果,所以我更愿意真正解决这个问题.
这是我将如何重写代码:
typedef struct DA {
string read_mode;
string data_type;
void *pValue;
void *pVarMemLoc;
}DA;
struct ROOM{
map<string, DA*> map;
};
struct DEPARTMENT{
map<string, ROOM> map;
};
struct FLOOR{
map<string, DEPARTMENT> map;
};
struct STAGE{
map<string, FLOOR> map;
};
struct LEVEL{
map<string, STAGE> map;
};
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它:
int main()
{
LEVEL DATA;
DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom1"] = new DA;
DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom2"] = new DA;
DATA.map["lvl1"].map["stg1"].map["flr1"].map["dep1"].map["rom3"] = new DA;
...
etc
Run Code Online (Sandbox Code Playgroud)
我的顾虑和最终解决方案主要来自MSDN.
If you intend to keep this monster of a data structure, there is little you can do about the warning other than disable it:
#pragma warning(disable:4503)
Run Code Online (Sandbox Code Playgroud)
其他人建议你如何禁用警告.我建议你重新考虑你的设计.使用比map ^ 5更多的抽象.或者更改存储的数据结构.例如,使用地图而不是地图^ 5.
更新:
我的意思是你基本上有两个选择:
您可以根据需要使用具有多个字符串/级别的键:
struct Key3 { std::string x, y, z; };
typedef std::map<Key3, DA*> MyMap;
或者你构建一些通用的东西,其中每个级别可以包含DA*值和/或另一个级别.