使用map容器,它的元素数据是键值和映照数据,形如<string,string>、<string,int>、<int,double>。使用时加一个#include< map >就行了。
以下用代码实现它的功能:
#include<map>
#include<string>
#include<iostream>
#pragma waring(disable:4786)
using namespace std;
int main()
{
/*map<string,float> m;
m["Jack"]=98.5;
m["Boin"]=96.0;
m["Kate"]=97.5;
map<string,float>::iterator t;//遍历
for(t=m.begin();t!=m.end();t++)
{
cout<<(*t).first<<": "<<(*t).second<<endl;
}*/
map<int,char> v;
v[25]='m';
v[28]='c';
v[3]='z';
map<int,char>::iterator t;//去重、排序
for(t=v.begin();t!=v.end();t++)
{
cout<<(*t).first<<": "<<(*t).second<<endl;
}
v.erase(28);//删除28这个元素
for(t=v.begin();t!=v.end();t++)
{
cout<<(*t).first<<": "<<(*t).second<<endl;
}
v[5]='k';
v[25]='m';
v[100]='l';
map<int,char>::reverse_iterator rt;
for(rt=v.rbegin();rt!=v.rend();rt++)
{
cout<<(*rt).first<<": "<<(*rt).second<<endl;
}
return 0;
}