STL之map

时间:2019-11-03
本文章向大家介绍STL之map,主要包括STL之map使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

C++ map

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

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中最简单最常用的插入添加!

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中的元素是自动按key升序排序,所以不能对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++;
    }

参考blog:https://www.cnblogs.com/jiading/p/11089999.html

原文地址:https://www.cnblogs.com/becase/p/11788093.html