【用法详解】C++--STL之map的用法详解与学习心得

STL之map的用法详解与学习心得

map是一种二叉树的数据存储结构。map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具备对数据自动排序的功能,因此在map内部全部的数据都是有序的(map中的元素是自动按Key升序排序的)
map的特色:  一、存储Key-value对
                          二、支持快速查找,查找的复杂度基本是Log(N)
                          三、快速插入,快速删除,快速修改记

1、map的构造函数

map共提供了6个构造函数,咱们一般用以下方法构造一个map:
map<string,int>mp;     //这里的mp就是本身取的名字。定义map类型的变量mp
还能够本身定义的map<int,int>mp;  
mp->first能够取得key值,mp->second能够取得value值;
map自动按照key值按升序排列,与数据插入的顺序无关,key的值不能修改,可是能够修改value的值。

2、数据的插入

第一种:用insert函数插入pair数据,下面举例说明:html

#include <iostream>
#include <map>
using namespace std;
int main() {
    map<int,string> mp;
    mp.insert(pair<int,string>(1,"Tom"));
    mp.insert(pair<int,string>(2,"Alice"));
    mp.insert(pair<int,string>(3,"Bob"));

    map<int,string>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
       cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}
第二种:用数组方式插入数据,下面举例说明:
#include <iostream>
#include <map>
using namespace std;
int main() {
    map<int,string> mp;
    mp[1]="Tom";
    mp[2]="Alice";
    mp[3]="Bob";
    map<int,string>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
       cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

第三种:用insert函数插入value_type数据,下面举例说明:

#include <iostream>
#include <map>
using namespace std;
int main() {
    map<int,string> mp;
    mp.insert(map<int, string>::value_type (1, "Tom"));
    mp.insert(map<int, string>::value_type (2, "Alice"));
    mp.insert(map<int, string>::value_type (3, "Bob"));
    mp.insert(map<int,string>::value_type(4,"Kim"));
    map<int,string>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

3、数据的遍历

这里也提供三种方法,对map进行遍历
第一种:应用前向迭代器;
第二种:应用反相迭代器;
第三种,用数组的形式输出;其中要用到size()函数判断map的大小:一般往map里面插入了数据,咱们怎么知道当前已经插入了多少数据呢,能够用size函数,用法以下:
int nSize = mp.size();
以上三种遍历方式的具体程序说明以下:
#include <iostream>
#include <map>
using namespace std;
int main() {
    map<int,string> mp;
    mp.insert(pair<int,string>(3,"Bob"));
    mp.insert(pair<int,string>(1,"Tom"));
    mp.insert(pair<int,string>(4,"John"));
    mp.insert(pair<int,string>(2,"Alice"));
    mp.insert(pair<int,string>(5,"Cody"));
    //前向迭代器
    map<int,string>::iterator it;
    for(it=mp.begin();it!=mp.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    //反相迭代器
    map<int,string>::reverse_iterator rit;
    for(rit=mp.rbegin();rit!=mp.rend();rit++){
        cout<<rit->first<<" "<<rit->second<<endl;
    }
    //数组遍历
    int nSize = mp.size();
    for(int nindex=1;nindex<=nSize;nindex++){
        //此处应注意,应该是 for(int nindex = 1; nindex <= nSize; nindex++)
        //而不是 for(int nindex = 0; nindex < nSize; nindex++)
        cout<<mp[nindex]<<endl;
    }
    return 0;
}

4、数据的查找

第一种:用find函数来定位数据出现位置,它返回的是一个迭代器。该方法获取的iterator数据类型是一个std::pair对象,包括两个数据 iterator->first和 iterator->second分别表明关键字和存储的数据值。
         当数据出现时,它返回数据所在位置的迭代器;若是map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。
        查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里须要提到的是begin()和end()两个成员,分别表明map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.
#include <iostream>
#include <map>
using namespace std;
int main() {
    //cout << "Hello, World!" << std::endl;
    map<int,string> mp;
    mp.insert(pair<int,string>(1,"Tom"));
    mp.insert(pair<int,string>(2,"Alice"));
    mp.insert(pair<int,string>(3,"Bob"));


    map<int,string>::iterator it;
    it = mp.find(3);
    if(it != mp.end())
        cout<<"Find, the value is "<<it->second<<endl;
    else
        cout<<"Do not Find"<<endl;

    return 0;
}

第二种:lower_bound函数和upper_bound函数来定位数据出现位置;
         l ower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
         upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
例如:map中已经插入了1,2,3,45的话,若是lower_bound(3)的话,返回的3,而upper-bound(3)的话,返回的就是4;
 Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,若是这两个迭代器相等的话,则说明map中不出现这个关键字。
#include <iostream>
#include <map>
using namespace std;
int main() {
    map<int,string> mp;
    mp.insert(pair<int,string>(1,"Tom"));
    mp.insert(pair<int,string>(2,"Alice"));
    mp.insert(pair<int,string>(3,"Bob"));
    mp.insert(pair<int,string>(4,"John"));
    mp.insert(pair<int,string>(5,"Cody"));
    map<int,string>::iterator it;

    it = mp.lower_bound(1);//返回的是下界为1的迭代器
    cout<<it->second<<endl;

    it = mp.lower_bound(2);//返回的是下界为2的迭代器
    cout<<it->second<<endl;

    it = mp.lower_bound(3);//返回的是下界为3的迭代器
    cout<<it->second<<endl;

    it = mp.upper_bound(2);//返回的是上界为3的迭代器
    cout<<it->second<<endl;

    it = mp.upper_bound(3);//返回的是上界为4的迭代器
    cout<<it->second<<endl;

    pair<map<int, string>::iterator, map<int, string>::iterator> mappair;

    mappair = mp.equal_range(2);

    if(mappair.first == mappair.second)
        cout<<"Do not Find"<<endl;
    else
        cout<<"Find"<<endl;

    mappair = mp.equal_range(3);
    if(mappair.first == mappair.second)
        cout<<"Do not Find"<<endl;
    else
        cout<<"Find"<<endl;

    return 0;
}