std::使用Visual C++2008映射未声明的标识符

std::unordered_map undeclared identifier using Visual C++ 2008

本文关键字:未声明 标识符 映射 C++2008 使用 Visual std      更新时间:2023-10-16
#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)
Visual C++2008在命名空间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。