map用法

1.map基本用法:


样例代码:

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
map<char,int> mymap;
map<char,int>::iterator it;
mymap['b'] = 100;//这表示map里存的单词‘b‘有100个
mymap['a'] = 200;
mymap['c'] = 300;
for(it=mymap.begin();it!=mymap.end();++it)
cout<<(*it).first<<"=>"<<(*it).second<<endl;//first指的是char类型的变量,second是int变量
return 0;
}



2.count用法:



size_type count(key_type x) const



注:如果mymap的key中有x,则函数返回1,否则返回0.



3.equal_range的用法:

pair<iterator,iterator>
equal_range ( const key_type& x );
pair<const_iterator,const_iterator>
equal_range ( const key_type& x ) const;


可以理解为该函数返回指向key为x的iterator.样例如下:

map<char,int> mymap;
pair<map<char,int>::iterator,map<char,int>::iterator> pRet;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
pRet = mymap.equal_range('a');
cout<<"lower bound points to : "<<endl;
cout<<pRet.first->first<<"=>"<<pRet.first->second<<endl;
cout<<pRet.second->first<<"=>"<<pRet.second->second<<endl;


4.erase与find的用法:

erase:

void erase ( iterator position );
size_type erase ( const key_type& x );
void erase ( iterator first, iterator last );

find:

iterator find ( const key_type& x );
const_iterator find ( const key_type& x ) const;

样例代码:

mymap.erase(mymap.find('b')); //删除map中key为'b'的记录


5.insert用法:

  pair<iterator,bool> insert ( const value_type& x );
iterator insert ( iterator position, const value_type& x );

  void insert ( iterator first, iterator last );

注: x为map的一条记录,包括key和value,而不单指value.

样例代码:

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
map<char,int> mymap;
pair<map<char,int>::iterator,bool> pRet;
map<char,int>::iterator it;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
mymap.insert(pair<char,int>('d',400));
mymap.insert(pair<char,int>('e',500));
pRet = mymap.insert(pair<char,int>('f',600));
if(pRet.second)
cout<<"Perfect!"<<endl;
it = mymap.begin();
mymap.insert(it,pair<char,int>('g',700));
mymap.insert(it,pair<char,int>('h',800));
map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('d'));

cout<<"mymap:"<<endl;
for(it=mymap.begin();it!=mymap.end();++it)
cout<<(*it).first<<"=>"<<(*it).second<<endl;
cout<<"anothermap:"<<endl;
for(it=anothermap.begin();it!=anothermap.end();++it)
cout<<(*it).first<<"=>"<<(*it).second<<endl;
return 0;
}

6.key_comp和value_comp用法:

key_compare key_comp ( ) const;

value_compare value_comp ( ) const;


样例代码:(key_comp)

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
int main()
{
map<char,int> mymap;
map<char,int>::key_compare mycomp;
map<char,int>::iterator it;
char highest;
mycomp = mymap.key_comp();
mymap['a']=100;
mymap['c']=200;
mymap['b']=300;
cout<<"mymap:"<<endl;
highest = mymap.rbegin()->first;
it = mymap.begin();
do
{
cout<<(*it).first<<"=>"<<(*it).second<<endl;
} while (mycomp((*it++).first,highest));
cout<<endl;
return 0;
}



注:该函数可用于map中对key排序(可参考上面的代码)



样例代码:(value_comp)

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#define Elem pair<char,int>
using namespace std;
int main()
{
map<char,int> mymap;
map<char,int>::iterator it;
Elem highest;
mymap['d']=100;
mymap['c']=200;
mymap['b']=300;
cout<<"mymap:"<<endl;
highest = *mymap.rbegin();
it = mymap.begin();
do
{
cout<<(*it).first<<"=>"<<(*it).second<<endl;
} while (mymap.value_comp()(*it++,highest));
cout<<endl;
return 0;
}



注:该函数也是对map的key排序。



7.lower_bound 和upper_bound的用法

iterator lower_bound ( const key_type& x );
const_iterator lower_bound ( const key_type& x ) const;

iterator upper_bound ( const key_type& x );
const_iterator upper_bound ( const key_type& x ) const;

注: lower_bound返回大于或等于x的第一个元素的iterator,upper_bound返回大于x的第一个元素的iterator.

样例代码:

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#define Elem pair<char,int>
using namespace std;
int main()
{
map<char,int> mymap;
map<char,int>::iterator it,itlow,itup;
Elem highest;
mymap['d']=100;
mymap['b']=200;
mymap['c']=300;
mymap['a']=400;
mymap['e']=500;
itlow = mymap.lower_bound('c');
itup = mymap.upper_bound('e');
mymap.erase(itlow,itup);
for (it=mymap.begin();it!=mymap.end();++it)
{
cout<<(*it).first<<"=>"<<(*it).second<<endl;
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值