//查找 map<int,int>::iterator pos = m.find(3); if(pos!=m.end()) { cout<<" GOT IT key = "<<(*pos).first<<"VALUE IS "<<(*pos).second<<endl; } int cnt = m.count(4); cout<<"KET IS 4 ="<<cnt<<endl; /* lower_bound(keyElem)*/ map<int,int>::iterator ret = m.lower_bound(3); if(ret!=m.end()) { cout<<"GOT IT! THE lower_bound 's key is :"<<ret->first<<" VALUE IS "<<ret->second<<endl; } else cout<<"SORRY NOT FOUND"<<endl;
/*upper_bound*/ ret = m.upper_bound(3); if(ret!=m.end()) { cout<<"GOT IT! THE upper_bound 's key is :"<<ret->first<<" VALUE IS "<<ret->second<<endl; } else cout<<"SORRY NOT FOUND"<<endl; /*equal_range */ pair<map<int,int>::iterator ,map<int,int>::iterator>it2 = m.equal_range(3); if(it2.first!=m.end()) { cout<<"GOT IT! THE lower_bound 's key is :"<<it2.first->first<<" VALUE IS "<<it2.first->second<<endl; } else { cout<<"SORRY NOT FOUND"<<endl; } if(it2.second!=m.end()) { cout<<"GOT IT! THE lower_bound 's key is :"<<it2.second->first<<" VALUE IS "<<it2.second->second<<endl; } else cout<<"SORRY NOT FOUND"<<endl; } voidtest04() { map<int,int,Mycompare> m;//有大变小 m.insert(pair<int,int>(1,10)); m.insert(make_pair(2,20)); m.insert(map<int,int>::value_type(3,30)); m[4] = 40; for(map<int,int>::iterator it = m.begin();it!=m.end();it++) { cout<<"KEY = "<<it->first<<" VALUE = "<<it->second<<endl; } } intmain() { // test02(); // test01(); // test03(); test04(); return0; }