【暖*墟】 #STL函数库# map和set的函数用法

  • set:集合中的对象不按特定的方式排序,并且没有重复对象。
  • 用法:存放的是对象的引用,没有重复对象。
  • map:一种把键和值映射的集合,它的每一个元素都包含一对键和值
  • 特征:map中的元素是自动按key(键值)升序排序。
  • 用法:从Map集合中检索元素时,只要给出键对象,就会返回对应的值对象。 

     目录

         1.count函数总结

  ***注意,count寻找的是键值,而不是(第二维度)存的数值***

         2.set中常用的函数

         3.map中常用的函数

(1) map最基本的构造函数

(2)map添加数据

(3)map中元素的查找

(4)map中元素的删除

(5)map 中 swap 的用法

(6)map的基本操作函数


1.count函数总结

map和set两种容器的底层结构都是红黑树,所以容器中不会出现相同的元素

map<int,int>sa;
//map<int,int>组成一个pair对象,第一个类型是键,第二个是值

set<int>sb;
//其中的count函数只判断键值的是否出现过(出现次数)
//map和set容器中不会出现相同的元素,因此count()的结果只能为0和1

count( )用来查找map,set中某个键值出现的次数。

***注意,是键值,而不是(第二维度)存的数值***

因此count( )的结果只能为0和1,可以以此来判断键值元素是否存在。

当然也可以使用find( )的方法判断键值是否存在。

拿map<key,value>举例:

find( )方法返回值是一个迭代器,成功时指向要查找的元素,失败时指向end。

count( )方法返回值是一个整数,1表示有,0表示没有。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main(){
    map<int,string>maps;

    if(maps.find(1)==maps.end()){
        cout<<"没有1这个元素"<<endl;
    }
    if(maps.count(1)==0){
        cout<<"没有1这个元素"<<endl;
    }
   
    maps[1]="one"; //添加元素1

    if(maps.find(1)!=maps.end()){
        cout<<"有1这个元素"<<endl;
    }
    if(maps.count(1)){
        cout<<"有1这个元素"<<endl;
    }

    return 0;
}

2.set中常用的函数

  • begin( ), 返回set容器的第一个元素.
  • end( ), 返回set容器的最后一个元素.
  • clear( ), 删除set容器中的所有的元素.
  • empty( ), 判断set容器是否为空.
  • max_size( ), 返回set容器可能包含的元素最大个数.
  • size( ), 返回当前set容器中的元素个数.
  • rbegin( ), 返回的值和end( )相同.
  • rend( ), 返回的值和begin( )相同.

c.begin() 返回一个迭代器,它指向容器c的第一个元素。

c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置。

c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素。

c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置。

------------------------------------反向迭代器的介绍请戳 这里

3.map中常用的函数

 map是关联式容器,包含“关键值对”,反映了键与值对应(映射)的关系。

 map中的元素是自动按key升序排序,所以不能对map用sort函数。

(1) map最基本的构造函数

  •    map<string , int >mapstring;        
  •    map<int ,string >mapint;
  •    map<sring, char>mapstring;        
  •    map< char ,string>mapchar;
  •    map<char ,int>mapchar;           
  •    map<int ,char >mapint;

(2)map添加数据

map<int ,string> maplive;  //定义类型
maplive.insert(pair<int,string>(102,"aclive"));
maplive.insert(map<int,string>::value_type(321,"hai"));
maplive[112]="April"; //map中最简单最常用的插入添加&&&

(3)map中元素的查找

find( )函数返回一个迭代器指向键值为key的元素的迭代器。

如果寻找失败就返回指向end的迭代器。       

map<int ,string >::iterator l_it; 
l_it=maplive.find(112);
if(l_it==maplive.end()) //迭代器返回指向end
    cout<<"Not find 112"<<endl;
else cout<<"I find 112"<<endl;

(4)map中元素的删除

   map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"Not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;

(5)map 中 swap 的用法

 Map 中的 swap 不是一个容器中的元素交换,而是两个容器交换!

#include <map>
#include <iostream>
using namespace std;

int main( ){
   map <int, int> m1, m2, m3;
   map <int, int>::iterator m1_Iter;

   m1.insert ( pair <int, int>  ( 1, 10 ) );
   m1.insert ( pair <int, int>  ( 2, 20 ) );
   m1.insert ( pair <int, int>  ( 3, 30 ) );
   m2.insert ( pair <int, int>  ( 10, 100 ) );
   m2.insert ( pair <int, int>  ( 20, 200 ) );
   m3.insert ( pair <int, int>  ( 30, 300 ) );

   cout << "The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter->second;
      cout   << "." << endl;

   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 ); //m2是参数映射map,m1是目标map

   cout << "After swapping with m2, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   cout << "After swapping with m2, map m2 is:";
   for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   // This is the specialized template version of swap
   swap( m1, m3 );

   cout << "After swapping with m3, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout   << "." << endl;
}

(6)map的基本操作函数

  •       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的函数

                                               ——时间划过风的轨迹,那个少年,还在等你。

猜你喜欢

转载自blog.csdn.net/flora715/article/details/82141797