map用法

来源:互联网 发布:海知智能 CTO 编辑:程序博客网 时间:2024/04/25 14:56

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;
   mymap['a'] = 200;
   mymap['c'] = 300;
   for(it=mymap.begin();it!=mymap.end();++it)
    cout<<(*it).first<<"=>"<<(*it).second<<endl;
   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;
}

 

8.begin,clear,empty,end,max_size,=,[],rbegin,rend,size,swap与vecotr、list中的同名函数用法一样。

 

原创粉丝点击