将multimap当作字典
#include<iostream>
#include<map>
#include<string>
#include<iomanip>
using namespace std;
void 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("smart","raffiniert"));
dict.insert(make_pair("strange" , "seltsam"));
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<<"\t"<<pos->second<<endl;
}
word=("raffiniert");
cout<<word<<":"<<endl;
for(pos=dict.begin(); pos !=dict.end(); ++pos){
if(pos->second==word){
cout<<"\t"<<pos->first<<endl;
}
}
}
搜索具有某特定实值(values)的元素:
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
template <class K,class V>
class value_equal{
private:
V value;
public:
value_equal(const V& v):value(v){}
bool operator()(pair<const K,V> elem){
return elem.second==value;
}
};
void 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]=6;
coll[6]=1;
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_equal<float ,float>(3.0));
if(pos != coll.end())
cout<<pos->first<<":"<<pos->second<<endl;
}