// stl/map1 #include < iostream > #include < map > #include < string > using namespace std; int main() ... { //StringFloatMap typedef map<string,float> StringFloatMap; StringFloatMap stocks; stocks["BASF"] = 369.50; stocks["VW"] = 413.50; stocks["Daimler"] = 819.00; stocks["BMW"] = 834.00; stocks["Siemens"] = 842.00; StringFloatMap::iterator pos; for(pos = stocks.begin();pos != stocks.end(); ++pos) ...{ cout <<"stocks: "<<pos ->first <<" " <<"price: " << pos ->second <<endl; } cout <<endl; for(pos = stocks.begin(); pos != stocks.end(); ++pos) ...{ pos ->second *= 2; } for(pos = stocks.begin();pos != stocks.end(); ++pos) ...{ cout <<"stocks: " << pos ->first<< " " <<"price: " << pos ->second <<endl; } cout <<endl; stocks["Volkswagen"] = stocks["VW"]; stocks.erase("VW"); for(pos == stocks.begin(); pos != stocks.end(); ++pos) ...{ cout <<"stock: "<< pos ->first <<" " <<"price: "<< pos ->second <<endl; }} /**/ /*result======================================stocks: BASF price: 369.5stocks: BMW price: 834stocks: Daimler price: 819stocks: Siemens price: 842stocks: VW price: 413.5stocks: BASF price: 739stocks: BMW price: 1668stocks: Daimler price: 1638stocks: Siemens price: 1684stocks: VW price: 827=======================================*/ // mmap1.cpp #include < iostream > #include < map > #include < string > #include < iomanip > using namespace std; int main() ... { typedef multimap<string,string> StrStrMMap; StrStrMMap dict; dict.insert(make_pair("day","Tag")); dict.insert(make_pair("strange","fremd")); dict.insert(make_pair("car","Auto")); dict.insert(make_pair("smart","elegant")); dict.insert(make_pair("trait","Merkmal")); dict.insert(make_pair("strange","seltsam")); dict.insert(make_pair("smart","raffiniert")); dict.insert(make_pair("smart","klug")); dict.insert(make_pair("clever","raffiniert")); StrStrMMap::iterator pos; cout.setf(ios::left,ios::adjustfield); cout <<' ' << setw(10) <<"English " <<"German "<<endl; cout <<setfill('-') <<setw(20) <<"" <<setfill(' ')<<endl; for(pos = dict.begin(); pos != dict.end();++pos) ...{ cout <<' ' <<setw(10) <<pos ->first.c_str() << pos ->second <<endl; } cout <<endl; string word("smart"); cout <<word<<": " <<endl; for(pos = dict.lower_bound(word); pos != dict.upper_bound(word); ++pos) ...{ cout <<" " << pos ->second <<endl; } word =("raffiniert"); cout <<word <<": " <<endl; for(pos = dict.begin(); pos != dict.end(); ++pos) ...{ if(pos ->second ==word) cout <<" "<<pos ->first<<endl; }} /**/ /*result=============================================== English German-------------------- car Auto clever raffiniert day Tag smart elegant smart raffiniert smart klug strange fremd strange seltsam trait Merkmalsmart: elegant raffiniert klugraffiniert: clever smart==================================================*/ // stl/mapfind.cpp /**/ /*================find Object================*/ #include < iostream > #include < algorithm > #include < map > using namespace std;template < class K, class V > class value_equals ... {private: V value;public: value_equals(const V& v) :value(v)...{} bool operator()(pair<const K,V> elem) ...{ return elem.second == value; }} ; int main() ... { typedef map<float,float> FloatFloatMap; FloatFloatMap coll; FloatFloatMap::iterator pos; coll[1] = 7; coll[2] = 4; coll[3] = 2; coll[4] = 3; coll[5] = 1; coll[6] = 6; coll[7] = 3; pos = coll.find(3.0); if(pos != coll.end())...{ cout <<pos ->first<<": " <<pos ->second<<endl; } pos = find_if(coll.begin(),coll.end(), value_equals<float,float>(3.0)); if(pos != coll.end()) ...{ cout <<pos ->first<<": " <<pos ->second<<endl; }} /**/ /*result====================3: 24: 3====================*/ // stl/mapcmp.cpp /**/ /*=================================================高级技巧1.如何使用maps2.写仿函数3.执行器定义排序准则4.比较字符串,不在乎大小写=================================================*/ #include < iostream > #include < iomanip > #include < map > #include < string > #include < algorithm > using namespace std; class RuntimeStringCmp ... {public: enum cmp_mode ...{normal,nocase};private: const cmp_mode mode; static bool nocase_compare(char c1,char c2) ...{ return toupper(c1) <toupper(c2); }public: RuntimeStringCmp(cmp_mode m = normal):mode(m) ...{} bool operator() (const string& s1,const string& s2)const ...{ if(mode == normal) return s1 < s2; else return lexicographical_compare(s1.begin(),s1.end(), s2.begin(),s2.end(),nocase_compare); }} ;typedef map < string , string ,RuntimeStringCmp > StringStringMap; void fillAndPrint(StringStringMap & coll); int main() ... { StringStringMap coll1; fillAndPrint(coll1); RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase); StringStringMap coll2(ignorecase); fillAndPrint(coll2);} void fillAndPrint(StringStringMap & coll) ... { coll["Deutshland"] = "Germanry"; coll["deutsh"] = "German"; coll["Hanken"] = "snag"; coll["arbeiten"] = "word"; coll["Hund"] = "dog"; coll["gehen"] = "go"; coll["Unternehmen"] = "enterprise"; coll["unternehmen"] = "undertake"; coll["gehen"] = "walk"; coll["Bestatter"] = "undertaker"; StringStringMap::iterator pos; cout.setf(ios::left,ios::adjustfield); for(pos=coll.begin(); pos != coll.end();++pos) ...{ cout <<setw(15)<<pos ->first.c_str() <<" " <<pos ->second <<endl; } cout <<endl;} /**/ /*result=============================Bestatter undertakerDeutshland GermanryHanken snagHund dogUnternehmen enterprisearbeiten worddeutsh Germangehen walkunternehmen undertakearbeiten wordBestatter undertakerdeutsh GermanDeutshland Germanrygehen walkHanken snagHund dogUnternehmen undertake===============================*/