map:
1).map属于关联容器(ok,又是容器,毫无疑问,又是和迭代器有关)
2).map用法:插入、遍历、查找、删除、清空、判断长度;
插入:
insert(pair<数型,数型>(参数变量,参数变量));
insert(map<数型,数型>::value_type(参变,参变));
数组
观察覆盖情况
遍历:
方法:使用迭代器访问
1.前向访问 2.反向访问3.数组
查找:
1.find() 2.count()3.lower_bound() upper_bound()
检查是否找到
(1)find:若存在,返后该元素所在位置的迭代器;否则返回end函数;
(2)count:判断是否存在该元素,存在,return1;否则return0;
(3)
map:
lower_bound(x)返回大于或等于x的第一个元素
upper_bound(x)返回大于x的第一个元素
pair:
Pair<map<int,string>::iterator,map<int,string>::iterator>mp;
* mp.equal_range(x);判断关键字x是否被覆盖
* 第一个变量是lower_bound返回的迭代器,第二个变量是upper_bound返回的迭代器
数据清空及判空 :
clear()及empty()
删除:erase( )
1.删除迭代器:
map<int,string>s;
map<int,string>::iterator t=find(x);
s.earse(t);
2.直接删除关键字
s.erase(x);
3.成片删除
s.erase(s.begin(),s.end());//前闭后开
map类型的查找:
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
map<int,string>m;
int main()
{
int n;string s;
cout<<"input n:"<<endl;
while(cin>>n)
{
m[1]="s1";m[2]="s2";m[4]="s4";m[5]="s5";
cout<<"size:"<<m.size()<<endl;
map<int,string>::iterator t=m.lower_bound(2);
map<int,string>::iterator t1=m.upper_bound(2);
cout<<t->first<<" "<<t->second<<endl;
cout<<t1->first<<" "<<t1->second<<endl;
}
}
用法总结eg:
#include<iostream>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int main()
{
map<int,string>m;
int x,n;string s;
while(cin>>n&&n)
{
cout<<"please input the number and name of the student:"<<endl;
cout<<"插入"<<endl;
cout<<"数组式插入"<<endl;
for(int i=0;i<n;i++)
{
cin>>x>>s;
m[x]=s;
}
cout<<"size:"<<m.size()<<endl;
cout<<"map式插入"<<endl;
for(int i=0;i<n;i++)
{
cin>>x>>s;
m.insert(map<int,string>::value_type(x,s));
}
cout<<"size:"<<m.size()<<endl;
cout<<"pair式插入"<<endl;
for(int i=0;i<n;i++)
{
cin>>x>>s;
m.insert(pair<int,string>(x,s));
}
cout<<"size:"<<m.size()<<endl;
cout<<"遍历"<<endl;
map<int,string>::iterator tt;
for(tt=m.begin();tt!=m.end();tt++)
cout<<tt->first<<" "<<tt->second<<endl;
cout<<"查找"<<endl;
cout<<"1.find"<<endl;
int num;
cout<<"input the number:"<<endl;
cin>>num;
map<int,string>::iterator t1=m.find(num);
if(t1!=m.end()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
cout<<"2.count"<<endl;
int flag=m.count(num);
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
cout<<"3.pair"<<endl;
pair<map<int,string>::iterator,map<int,string>::iterator> p;
p=m.equal_range(num);
if(p.first==p.second) cout<<"No find"<<endl;
else cout<<"Find"<<endl;
cout<<"删除"<<endl;
cout<<"1.迭代器删除"<<endl;
cout<<"input the num:"<<endl;
cin>>num;
map<int,string>::iterator t2=m.find(num);
m.erase(t2);
map<int,string>::iterator t3;
for(t3=m.begin();t3!=m.end();t3++)
cout<<t3->first<<" "<<t3->second<<endl;
cout<<"2.关键字删除"<<endl;
cout<<"input the num:"<<endl;
cin>>num;
m.erase(num);
map<int,string>::iterator t4;
for(t4=m.begin();t4!=m.end();t4++)
cout<<t4->first<<" "<<t4->second<<endl;
cout<<"3.整块删除"<<endl;
m.erase(m.begin(),m.end());
map<int,string>::iterator t5;
for(t5=m.begin();t5!=m.end();t5++)
cout<<t5->first<<" "<<t5->second<<endl;
cout<<"size:"<<m.size()<<endl;
cout<<"清空"<<endl;
m.clear();
cout<<"size:"<<m.size();
}
}