简体   繁体   中英

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

#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;
}

I am learning and can't figure it out. The compiler throws the error that type unordered_map is undeclared.

I am using Visual C++ 2008 Express Edition.

In Visual Studio 2008 the classes in Technical Report 1 (TR1) are in namespace std::tr1 . Add:

using namespace std::tr1;

to your code.

In the TR1 unordered_map is available from the <tr1/unordered_map> header file as std::tr1::unordered_map .

In the upcoming C++0x standard it is available from the <unordered_map> header file as std::unordered_map .

so you should use <tr1/unordered_map> header and std::tr1::unordered_map namespace for vc 2008 because vc 2008 does not support C++0x.

To answer the problem you quoted in comment.
Also, Make sure you download the feature pack for VS2008 !

Check under new features supported list.

New containers (tuple, array, unordered set, etc)

Visual C++ 2008 declares unordered_map in namespace std::tr1 , not in std . See http://msdn.microsoft.com/en-us/library/bb982522(VS.90).aspx , section Requirements .

Your code is working as intended in VS2010. With output of TWO. if that is what you are not getting. May be you should switch to VC++ 2010 Express Edition.
Probably VC++ 2008 does not include TR1

In C++03, unordered_map is defined in std::tr1 namespace (if its defined at all).

So you should use:

std::tr1::unordered_map<int, std::string> m;

Perhaps you are looking for stdext::hash_map instead (included in <hash_map> )?

VC++ 2008 express to my knowledge does not include TR1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM