Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 444
munish Avatar asked Jun 11 '11 06:06

munish


People also ask

What does Unordered_map return if key not found?

Return value The unordered_map::find() returns one of the two values: Iterator: An iterator to the element when the key exists in the unordered map. Iterator: An iterator to the end of the map when the key does not exist in the unordered map.

What is the difference between std :: map and std :: Unordered_map?

map is used to store elements as key,value pairs in sorted order. unordered_map is used to store elements as key,value pairs in non-sorted order.

For which use cases Map will win over Unordered_map?

If you are going to build large table once and do lots of queries, use std::unordered_map. If you are going to build small table (may be under 100 elements) and do lots of queries, use std::map . This is because reads on it are O(log n) . If you are going to change table a lot then may be std::map is good option.

What library is Unordered_map in?

C++ Library - <unordered_map>


2 Answers

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

using namespace std::tr1;

to your code.

like image 60
MW_dev Avatar answered Sep 21 '22 21:09

MW_dev


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)
like image 35
Alok Save Avatar answered Sep 24 '22 21:09

Alok Save