// Simple example showing hash_map in GCC 3.4.2 // and what the new C++ standard TR1 uses. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf #include // In TR1, include #include using namespace std; // In TR1, use std::tr1::unordered_map using __gnu_cxx::hash_map; // In TR1, use unordered_map typedef hash_map NameIntMap; // The rest of the code is unchanged. // Note: in GCC 3.4.2, you can't use a C++ string for a key // until you define hash() for strings. See: // http://gcc.gnu.org/ml/libstdc++/2002-04/msg00107.html 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; }