Question

#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.

Was it helpful?

Solution

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

using namespace std::tr1;

to your code.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top