使用map和multimap需要注意以下几点:
1.map和multimap将key/value(键值/实值 对组)当做元素,进行管理。它们可根据key的排序准则自动将元素元素排序。multimap允许重复元素,map不允许。
2.map的第一个参数被当做元素的key,第二个参数被当做元素的value,第三个参数用它来定义排序准则。元素的次序由它们的key决定,和value无关。
3.map和multimap根据元素的key自动对元素进行排序,这么一来,根据已知的key搜寻某个元素时,就能够有很好的性能,而根据已知value搜寻元素时,性能就很糟糕。
4.“自动排序”这一性质使得map和multimap身上有了一条重要的限制:不可以直接改变元素的key,因为这会破坏正确次序。要修改元素的key,必须先移除拥有该key的元素,
然后插入拥有新的key/value的元素。从迭代器的角度来看,元素的key是常数,至于元素的value倒是可以直接修改。
5.map和multimap不支持元素直接存取,因此元素的存取通常是经由迭代器进行。不过map有个例外,map提供下标操作符,可直接存取元素。
6.在map和multimap中,key被视为常数,因此不能调用任何变动性算法,例如你不能对它们调用remove(),因为remove()算法实际上是以一个参数值覆盖被移除的元素。如果要移除map和multimap的元素,你只能使用它们所提供的成员函数。
7.将map视为关联式数组:
关联式容器并不提供元素的直接存取,你必须依靠迭代器,map是个例外。Non-const map提供下标操作符,支持下标操作符。支持元素的直接存取,不过下标操作符的索引值并非元素整数位置,而是元素的key,也就是说索引可以是任意型别,而非局限为整数型别。
8.map和一般数组之间的区别不仅仅在于索引型别,其他的区别包括:
如果你使用某个key作为索引,而容器之中尚未存在对应元素,那么就会自动安插该元素,新元素的value由default构造函数构造。
优点是:你可以透过更方便的接口对着map安插新元素。
缺点是:你可能不小心误置新元素。
代码示例:
//map修改key&value
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
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.20;
StringFloatMap::iterator pos;
for(pos=stocks.begin();pos!=stocks.end();++pos)
{
cout<<"stock: "<<pos->first<<"\t"
<<"price: "<<pos->second<<endl;
}
cout<<endl;
for(pos=stocks.begin();pos!=stocks.end();++pos)
{
pos->second*=2;//修改value
}
for(pos=stocks.begin();pos!=stocks.end();++pos)
{
cout<<"stock: "<<pos->first<<"\t"
<<"price: "<<pos->second<<endl;
}
cout<<endl;
stocks["Volkswagen"]=stocks["VW"];//修改key
stocks.erase("VW");//删除原来的
for(pos=stocks.begin();pos!=stocks.end();++pos)
{
cout<<"stock: "<<pos->first<<"\t"
<<"price: "<<pos->second<<endl;
}
cout<<endl;
system("pause");
return 0;
}
运行结果:
//查找
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
#include<iomanip>
using namespace std;
void print()
{
cout.setf(ios::left,ios::adjustfield);
cout<<" "<<setw(10)<<"english "<<"german "<<endl;
cout<<setfill('-')<<setw(20)<<""<<setfill(' ')<<endl;
}
int main()
{
typedef multimap<string,string> StrStrMap;
StrStrMap dict;
dict.insert(make_pair("day","Tag"));
dict.insert(make_pair("strange","fremd"));
dict.insert(make_pair("smart","elegant"));
dict.insert(make_pair("trait","Merkmal"));
dict.insert(make_pair("car","Auto"));
dict.insert(make_pair("strange","seltsam"));
dict.insert(make_pair("smart","raffiniert"));
dict.insert(make_pair("smart","klug"));
dict.insert(make_pair("clever","raffiniert"));
StrStrMap::iterator pos;
print();
for(pos=dict.begin();pos!=dict.end();++pos)
{
cout<<" "<<setw(10)<<pos->first<<pos->second<<endl;
}
cout<<endl;
string word("smart");
cout<<word<<": "<<endl;
for(pos=dict.lower_bound(word);pos!=dict.upper_bound(word);++pos)//查找key
{
cout<<" "<<pos->second<<endl;
}
word="raffiniert";
cout<<word<<": "<<endl;
for(pos=dict.begin();pos!=dict.end();++pos)
{
if(pos->second==word)//查找value
{
cout<<" "<<pos->first<<endl;
}
}
cout<<endl;
word="day";
pos=dict.find(word);//key
if(pos!=dict.end())
{
dict.erase(pos);//删除key
}
print();
for(pos=dict.begin();pos!=dict.end();++pos)
{
cout<<" "<<setw(10)<<pos->first<<pos->second<<endl;
}
cout<<endl;
word="raffiniert";
for(pos=dict.begin();pos!=dict.end();)
{
if(pos->second==word)
{
dict.erase(pos++);
}
else
{
++pos;
}
}
print();
for(pos=dict.begin();pos!=dict.end();++pos)
{
cout<<" "<<setw(10)<<pos->first<<pos->second<<endl;
}
cout<<endl;
system("pause");
return 0;
}
运行结果(查找部分):
运行结果(删除部分):