map操作

来源:互联网 发布:国外大学图书馆 知乎 编辑:程序博客网 时间:2024/05/24 06:54

注意:

C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。

map有去重功能,以最后一个为准。

map有自动排序功能,不能用sort。

map的swap有两种交换。

map的count函数返回只有0或1,只有multimap中是0,1,>1多种。

1,map类型的创建:

map<a,b> maps;

其中a,b可以由string,char,int.....相互组合。

2,map类型的插入:

最简单的是:如:

    

map<string,int> maps1;    //string到int    maps1["abc"]=777;

还可以用insert函数:如:

map<int,char> m3;        //int到charm3.insert ( pair <int, char>  ( 3, 'i' ) );

3,删除,erase();

map<string,char> maps;    string s; char ch;    cin>>s>>ch;    maps[s]=ch;    maps["5"]='k';    maps["1"]='u';    maps["3"]='w';    maps["2"]='q';    map<string,char>::iterator l_it;    for(l_it=maps.begin();l_it!=maps.end();++l_it)    {        cout<<l_it->first;        printf("    %c\n",l_it->second);    }    l_it=maps.find("3");    maps.erase(l_it);    for(l_it=maps.begin();l_it!=maps.end();++l_it)    {        cout<<l_it->first;        printf("    %c\n",l_it->second);    }

4,查找,find();返回的是迭代器

map<char,int> maps1;       //查找    maps1['k']=21312312;    map<char,int>::iterator l_it;    l_it=maps1.find('k');    if(l_it==maps1.end())        printf("没找到\n");    else        printf("%c   %d\n",*l_it,l_it->second);

5,交换,swap()

(1),一个对象里的元素交换;

   

map<int,char> maps;      //交换    maps[5]='k';    maps[1]='u';    maps[3]='w';    maps[2]='q';    map<int,char>::iterator l_it;    for(l_it=maps.begin();l_it!=maps.end();++l_it)    {        cout<<l_it->first;        printf("    %c\n",l_it->second);    }    printf("-----------------\n");    swap(maps[5],maps[1]);    for(l_it=maps.begin();l_it!=maps.end();++l_it)    {        cout<<l_it->first;        printf("    %c\n",l_it->second);    }   
 (2)两个对象相互交换。

   map<int,char> maps,m2;      //交换    maps[5]='k';    maps[1]='u';    maps[3]='w';    maps[2]='q';    map<int,char>::iterator l_it;    m2.insert ( pair <int, char>  ( 10, 'z' ) );    cout<<m2.size()<<endl;            //size()    maps.swap(m2);    cout<<m2.size()<<endl;    for(l_it=m2.begin();l_it!=m2.end();++l_it)    {        cout<<l_it->first;        printf("    %c\n",l_it->second);    }

6,clear(),empty();

map<char,int> maps;              //clear();empty();    map<char,int>::iterator l_it;    maps['5']=91;    maps['1']=32;    maps['3']=3;    maps['2']=779;    if(maps.empty())    {        printf("为空\n");    }    else        printf("不为空\n");    maps.clear();    if(maps.empty())    {        printf("为空\n");    }    else        printf("不为空\n");   

7,count();

map<int,char> maps;              //count();结果只有0,1;返回key出现的个数,这个就是:1,2,3,4    map<int,char>::iterator l_it;    //multimap中是0,1,>1多种    maps[2]='q';    maps[1]='w';    maps[4]='q';    maps[3]='o';    cout<<maps.count(3)<<endl;

8,lower_bound()返回一个迭代器,指向map中键值 >= key的第一个元素。,max_size()

map<char,int> maps;              //lower_bound(),max_size()    map<char,int>::iterator l_it;    maps['5']=91;    maps['1']=32;    maps['3']=3;    maps['3']=779;    l_it=maps.lower_bound('2');    printf("%c   %d\n",l_it->first,l_it->second);    cout<<maps.max_size()<<endl;

9,upper_bound();返回一个迭代器,指向map中键值>key的第一个元素。

 map<int,char> maps;              //upper_bound();    map<int,char>::iterator l_it;    maps[2]='q';    maps[1]='w';    maps[4]='q';    maps[3]='o';    l_it=maps.upper_bound(2);    printf("%d   %c\n",l_it->first,l_it->second);

10,map的基本操作函数:
      C++ Maps是一种关联式容器,包含“关键字/值”对
      begin()          返回指向map头部的迭代器
      clear()         删除所有元素
      count()          返回指定元素出现的次数
      empty()          如果map为空则返回true
      end()            返回指向map末尾的迭代器
      equal_range()    返回特殊条目的迭代器对
      erase()          删除一个元素
      find()           查找一个元素
      get_allocator()  返回map的配置器
      insert()         插入元素
      key_comp()       返回比较元素key的函数
      lower_bound()    回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()           返回map中元素的个数
      swap()            交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数


0 0
原创粉丝点击