It is a special map, the key query complexity is O (1), but the map for keys complexity is O (log N)
Some compiler to add the following header file when using:
#if(__cplusplus == 201103L) #include <unordered_map> #include <unordered_set> #else #include <tr1/unordered_map> #include <tr1/unordered_set> namespace std { using std::tr1::unordered_map; using std::tr1::unordered_set; } #endif
Let's look at several functions:
#include <bits/stdc++.h> #if(__cplusplus == 201103L) #include <unordered_map> #include <unordered_set> #else #include <tr1/unordered_map> #include <tr1/unordered_set> namespace std { using std::tr1::unordered_map; using std::tr1::unordered_set; } #endif using namespace std; const int maxn = 1e5 + 100; unordered_map<int, int> fa; int main() { fa[. 1 ] = 2 ; FA [ 2 ] = . 3 ; FA [ . 3 ] = . 4 ; FA [ . 4 ] = 0 ; // COUNT query function key has a value 0 or returns. 1 COUT << fa.count ( . 5 ) < <endl; // 0 COUT << fa.count ( . 4 ) << endl; // . 1 COUT << fa.count ( . 3 ) << endl; // . 1 IF (fa.find ( . 1 ) == FA. End ()) << COUT " absent " << endl; // presence of the else COUT <<" Presence " << endl; IF (fa.find ( . 4 ) fa.end == ()) << COUT " absent " << endl; // presence of the else COUT << " presence " << endl; IF ( fa.find ( . 5 ) fa.end == ()) << COUT " absent " << endl; // absent the else COUT << " presence " << endl; // iterate FA unordered_map < int , int > :: iterator it; for(IT = fa.begin (); IT =! fa.end ();) { COUT << IT-> First << " " << IT-> SECOND << endl; ++ IT; } COUT << endl << endl; // remove a key element of an odd number for (IT = fa.begin ();! IT = fa.end ();) { IF (IT-> First% 2 == . 1 ) IT fa.erase = (IT); // then points to the next delete the else ++ IT; } for (IT = fa.begin (); IT =! fa.end ();) { COUT << IT-> First < < " "<<it->second<<endl; ++it; } return 0; }