// File: map4.C // // Testing the STL map class. #include #include #include #include // C string library #include // STL map #include // STL hash table #include // STL string class //====================================================================== // A class defined so we can have a function object // to compare months. class monthlessthan { public: monthlessthan() ; bool operator() (const string& s1, const string& s2) const { return order[s1] < order[s2] ; } private: static hash_map order ; } ; hash_map monthlessthan::order ; monthlessthan::monthlessthan() { if (order.size() != 0) return ; order["january"] = 1 ; order["february"] = 2 ; order["march"] = 3 ; order["april"] = 4 ; order["may"] = 5 ; order["june"] = 6 ; order["july"] = 7 ; order["august"] = 8 ; order["september"] = 9 ; order["october"] = 10 ; order["november"] = 11 ; order["december"] = 12 ; } //====================================================================== typedef map mmap; int main() { mmap monthinfo; monthinfo["january"] = 31; monthinfo["february"] = 28; monthinfo["march"] = 31; monthinfo["april"] = 30; monthinfo["may"] = 31; monthinfo["june"] = 30; monthinfo["july"] = 31; monthinfo["august"] = 31; monthinfo["september"] = 30; monthinfo["october"] = 31; monthinfo["november"] = 30; monthinfo["december"] = 31; mmap::iterator it ; for (it = monthinfo.begin() ; it != monthinfo.end() ; it++) { cout << (*it).first << " has " ; cout << (*it).second << " days" << endl ; } }