STL之map

 

C++ map

  map是C++中的一个标准容器,ta提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果。

特点:

  •   自动建立Key-value的对应。key 和 value可以是任意你需要的类型。
  •   其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值。
  •   头文件:#include <map> //注意,STL头文件没有扩展名.h。
  •   map对象是模板类,需要关键字和存储对象两个模板参数:std:map<int,string> personnel 这样就定义了一个用int作为索引,并拥有相关联的指向string的指针。  

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添加数据;

1  map<int ,string> maplive;  
//第一种方法:
2 1.maplive.insert(pair<int,string>(102,"aclive"));
//第二种方法:
3 2.maplive.insert(map<int,string>::value_type(321,"hai"));
//第三种方法:
4 3, maplive[112]="April";//map中最简单最常用的插入添加!

 p.s:用insert函数插入数据,在数据的 插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值。

3、map中元素的查找:

   find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。        

1  map<int ,string >::iterator l_it;; 
2    l_it=maplive.find(112);
3    if(l_it==maplive.end())
4                 cout<<"we do not find 112"<<endl;
5    else cout<<"wo find 112"<<endl;

 

4、map中元素的删除:

   如果删除112;

1  map<int ,string >::iterator l_it;;   
2    l_it=maplive.find(112);
3    if(l_it==maplive.end())
4         cout<<"we do not find 112"<<endl;
5    else  maplive.erase(l_it);  //delete 112;

5、map中 swap的用法:

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

  示例:

 1  #include <map> 
 2  #include <iostream>
 3   using namespace std;
 4   int main( )
 5   {
 6       map <int, int> m1, m2, m3;
 7       map <int, int>::iterator m1_Iter;
 8       m1.insert ( pair <int, int>  ( 1, 10 ) );
 9       m1.insert ( pair <int, int>  ( 2, 20 ) );
10       m1.insert ( pair <int, int>  ( 3, 30 ) );
11       m2.insert ( pair <int, int>  ( 10, 100 ) );
12       m2.insert ( pair <int, int>  ( 20, 200 ) );
13       m3.insert ( pair <int, int>  ( 30, 300 ) );
14    cout << "The original map m1 is:";
15    for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
16       cout << " " << m1_Iter->second;
17       cout   << "." << endl;
18    // This is the member function version of swap
19    //m2 is said to be the argument map; m1 the target map
20    m1.swap( m2 );
21    cout << "After swapping with m2, map m1 is:";
22    for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
23       cout << " " << m1_Iter -> second;
24       cout  << "." << endl;
25    cout << "After swapping with m2, map m2 is:";
26    for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
27       cout << " " << m1_Iter -> second;
28       cout  << "." << endl;
29    // This is the specialized template version of swap
30    swap( m1, m3 );
31    cout << "After swapping with m3, map m1 is:";
32    for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
33       cout << " " << m1_Iter -> second;
34       cout   << "." << endl;
35 }

6、map的sort问题:

  •        map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。所以不能对map用sort函数:

  示例:

 1  #include <map>  #include <iostream>
 2  using namespace std;
 3  int main( )
 4  {
 5    map <int, int> m1;
 6    map <int, int>::iterator m1_Iter;
 7    m1.insert ( pair <int, int>  ( 1, 20 ) );
 8    m1.insert ( pair <int, int>  ( 4, 40 ) );
 9    m1.insert ( pair <int, int>  ( 3, 60 ) );
10    m1.insert ( pair <int, int>  ( 2, 50 ) );
11    m1.insert ( pair <int, int>  ( 6, 40 ) );
12    m1.insert ( pair <int, int>  ( 7, 30 ) );
13    cout << "The original map m1 is:"<<endl;
14    for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
15       cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;
16 }

7、map的基本操作函数:

    C++ Maps是一种关联式容器,包含“关键字/值”对

    begin()          返回指向map头部的迭代器

    clear()         删除所有元素

    begin()          返回指向map头部的迭代器

    clear()         删除所有元素

    count()          返回指定元素出现的次数

    empty()          如果map为空则返回true

    end()            返回指向map末尾的迭代器

    equal_range()    返回特殊条目的迭代器对

    erase()          删除一个元素

    find()           查找一个元素(查找的速度为O(logn),速度很快)

    get_allocator()  返回map的配置器

    insert()         插入元素

    key_comp()       返回比较元素key的函数

    lower_bound()    返回键值>=给定元素的第一个位置

    max_size()       返回可以容纳的最大元素个数

    rbegin()         返回一个指向map尾部的逆向迭代器

    rend()           返回一个指向map头部的逆向迭代器

    size()           返回map中元素的个数

    swap()            交换两个map

    upper_bound()     返回键值>给定元素的第一个位置

    value_comp()      返回比较元素value的函数

8.遍历:map的遍历是通过迭代器完成的:

遍历过程:

//第一种:应用前向迭代器
map<int, int>::iterator iter; iter = _map.begin(); while(iter != map.end()) { cout << iter->first << " : " << iter->second << endl; iter++; }

//第二种:用数组方式,程序说明如下

#include <map> #include <string>

#include <iostream> using namespace std;

int main() {

map<int, string> mapStudent;

mapStudent.insert(pair<int, string>(1, "student_one"));

mapStudent.insert(pair<int, string>(2, "student_two"));

mapStudent.insert(pair<int, string>(3, "student_three"));

int nSize = mapStudent.size(); //此处应注意,应该是 for(int nindex = 1; nindex <= nSize; nindex++)

//而不是 for(int nindex = 0; nindex < nSize; nindex++)

for(int nindex = 1; nindex <= nSize; nindex++)

cout<<mapStudent[nindex]<<endl;

}

参考blog:

https://www.cnblogs.com/jiading/p/11089999.html

https://www.cnblogs.com/ZY-Dream/p/10037931.html

星河滚烫,你是人间理想。
原文地址:https://www.cnblogs.com/becase/p/11788093.html