#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map< int, string > m;
m[1] = "one";
m[2] = "two";
m[4] = "four";
m[3] = "three";
m[2] = "TWO!";
cout << m[2] << endl;
m.clear();
return 0;
}
我在学习,却搞不明白。编译器抛出类型unordered_map
未声明的错误。
我使用的是Visual C++2008速成版。
在Visual Studio 2008中,技术报告1(TR1(中的类位于命名空间std::TR1中。添加:
using namespace std::tr1;
到您的代码。
在TR1中,unordered_map
可作为std::tr1::unordered_map
从<tr1/unordered_map>
头文件中获得。
在即将推出的C++0x标准中,它可以从<unordered_map>
头文件中作为std::unordered_map
获得。
因此您应该为vc 2008使用<tr1/unordered_map>
标头和std::tr1::unordered_map
命名空间,因为vc 2008不支持C++0x。
回答你在评论中引用的问题
此外,请确保下载VS2008的功能包!
在支持的新功能列表下进行检查。
New containers (tuple, array, unordered set, etc)
std::tr1
中声明了unordered_map
,而不是在std
中。看见http://msdn.microsoft.com/en-us/library/bb982522(VS.90(.aspx,要求一节。
您的代码在VS2010中按预期工作。输出两个!如果这是你没有得到的。可能您应该切换到VC++2010速成版
可能VC++2008不包括TR1
在C++03中,unordered_map
是在std::tr1
命名空间中定义的(如果已经定义的话(。
所以你应该使用:
std::tr1::unordered_map<int, std::string> m;
也许您正在寻找stdext::hash_map
(包含在<hash_map>
中(?
据我所知,VC++2008表达不包括TR1。