// Simple example showing unordered_map // In the next C++ standard, this will // be in std, not std::tr1. See // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf #include #include using namespace std; // In gcc 3, you need to replace the lines below // with: // // #include // typedef __gnu_cxx::hash_map NameIntMap; // // and you have to use const char * or define a // hash() function for strings. See // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html #include typedef std::tr1::unordered_map NameIntMap; int main( ) { NameIntMap ageMap; ageMap["John"] = 24; ageMap["Mary"] = 32; cout << "Number of entries: " << ageMap.size() << endl << "John is " << ageMap["John"] << endl << "Mary is " << ageMap["Mary"] << endl; for (NameIntMap::iterator iter = ageMap.begin(); iter != ageMap.end(); ++iter) { cout << "Age of " << iter->first << " is " << iter->second << endl; } return 0; }