#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.
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.
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.
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.
C++ Library - <unordered_map>
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With