C++STL之map映照容器

map映照容器
map映照容器的元素数据是由一个键值和一个映照数据组成的, 键值与映照数据之间具有一一映照关系.
map映照容器的数据结构也是采用红黑树来实现的, 插入元素的键值不允许重复, 比较函数只对元素的键值进行比较, 元素的各项数据可通过键值检索出来. 由于map与set采用的都是红黑树的数据结构, 所以, 用法基本相似.
 
键值                映照数据
Name             Score
Jack                 98.5
Bomi               96.0
Kate                97.5
 
使用map容器需要包含头文件"#include<map>", map文件包含了对multimap多重映照容器的定义.
 
1.1map创建, 插入元素和遍历访问
创建map对象, 键值与映照数据的类型由自己去定义. 在没有指定比较函数时, 元素的插入位置是按键值由小到大到红黑树中去的, 这点和set一样. 下面这个程序详细的说明了如何操作map容器.
#include<map>
#include<string>
#include<iostream>
using namespace std;
 
int main()
{
    //定义map对象, 当前没有任何元素
    map<string, float> m;
    //插入元素, 按键值的由小到大放入红黑树中
    m["Jack"] = 98.5;
    m["Bomi"] = 96.0;
    m["Kate"] = 97.5;
    //向前遍历元素
    map<string, float>::iterator it;
    for(it = m.begin(); it != m.end(); it++)
    {
        //输出键值与映照数据
        cout << (*it).first << " : " << (*it).second << endl;
    }
    return 0;
}
/*
Bomi : 96
Jack : 98.5
Kate : 97.5
*/
 
1.2删除元素
与set容器一样, map映照容器的erase()删除元素函数, 可以删除某个迭代器位置上的元素, 等于某个键值的元素, 一个迭代器区间上的所有元素, 当然, 也可以使用clear()方法清空map映照容器.
下面这个程序演示了删除map容器中键值为28的元素:
#include<map>
#include<string>
#include<iostream>
using namespace std;
 
int main()
{
    //定义map对象, 当前没有任何元素
    map<int , char> m;
    //插入元素, 按键值的由小到大放入红黑树中
    m[25] = 'm';
    m[28] = 'k';
    m[10] = 'x';
    m[30] = 'a';
    //删除值为28的元素
    m.erase(28);
    //前向遍历元素
    map<int, char>::iterator it;
    for(it = m.begin(); it != m.end(); it++)
    {
        //输出键值与映照数据
        cout << (*it).first << " : " << (*it).second << endl;
    }
    return 0;
}
 
/*
10 : x
25 : m
30 : a
*/
 
1.3元素反向遍历
可以使用反向迭代器reverse_iterator反向遍历map映照容器中的数据, 它需要rbegin()方法和rend()方法指出反向遍历的起始位置和终止位置.
#include<map>
#include<string>
#include<iostream>
using namespace std;
 
int main()
{
    //定义map对象, 当前没有任何元素
    map<int,char> m;
    //插入元素, 按键值的由小到大放入红黑树中
    m[25] = 'm';
    m[28] = 'k';
    m[10] = 'x';
    m[30] = 'a';
    //反向遍历元素
    map<int, char>::reverse_iterator rit;
    for(rit = m.rbegin(); rit != m.rend(); rit++)
    {
        //输出键值与映照数据
        cout << (*rit).first << " : " << (*rit).second << endl;
    }
    return 0;
}
/*
30 : a
28 : k
25 : m
10 : x
*/
 
1.4元素的搜索
使用find()方法来搜索某个键值, 如果搜索到了, 则返回该键值所在的迭代器位置, 否则, 返回end()迭代器位置. 由于map采用红黑树数据结构来实现, 所以搜索速度是极快的.
下面这个程序搜索键值为28的元素:
#include<map>
#include<string>
#include<iostream>
using namespace std;
 
int main()
{
    map<int, char> m;
    //插入元素, 按键值的由小到大顺序放入红黑树中
    m[25] = 'm';
    m[28] = 'k';
    m[10] = 'x';
    m[30] = 'a';
    map<int, char>::iterator it;
    it = m.find(28);
    if(it != m.end())
    {
        cout << (*it).first << " : " << (*it).second << endl;
    }
    else
    {
        cout << "not found it" << endl;
    }
    return 0;
}
 
/*
28 : k
*/
 
1.5自定义比较函数
将元素插入到map中去的时候, map会根据设定的比较函数将该元素放到该放的节点上去. 在定义map的时候, 如果没有指定比较函数, 那么采用默认的比较函数, 即按键值由小到大的顺序插入元素. 在很多情况下, 需要自己编写比较函数.
编写比较函数与set比较函数是一致的, 因为它们的内部数据结构都是红黑树. 编写方法有两种:
(1)如果元素不是结构体, 那么, 可以编写比较函数规则是要求按键值由大到小的顺序将元素插入到map中:
#include<map>
#include<string>
#include<iostream>
using namespace std;
 
//自定义比较函数myComp
struct myComp
{
    bool operator() (const int &a, const int &b)
    {
        if(a != b) return a > b;
        else    return a > b;
    }
};
 
int main()
{
    //定义map对象, 当前没有任何元素
    map<int, char, myComp> m;
    //插入元素, 按键值的由小到大放入红黑树中
    m[25] = 'm';
    m[28] = 'k';
    m[10] = 'x';
    m[30] = 'a';
    //使用前向迭代器中序遍历map
    map<int, char, myComp>::iterator it;
    for(it = m.begin(); it != m.end(); it++)
    {
        cout << (*it).first << " : " << (*it).second << endl;
    }
    return 0;
}
 
/*
30 : a
28 : k
25 : m
10 : x
*/
 
(2)如果元素是结构体, 那么, 可以直接把比较函数写在结构体内. 下面的程序详细说明了如何操作:
#include<map>
#include<string>
#include<iostream>
using namespace std;
 
struct Info
{
    string name;
    float score;
    //重载"<"操作符, 自定义排序规则
    bool operator < (const Info &a) const
    {
        //按score由大到小排列. 如果要由小到大排列, 使用">"号即可
        return a.score < score;
    }
};
 
int main()
{
    //定义map对象, 当前没有任何元素
    map<Info, int> m;
    //定义Info结构体变量
    Info info;
    //插入元素, 按键值的由小到大放入红黑树中
    info.name = "Jack";
    info.score = 60;
    m[info] = 25;
    info.name = "Bomi";
    info.score = 80;
    m[info] = 10;
    info.name = "Peti";
    info.score = 66.5;
    m[info] = 30;
    //使用前向迭代器中序遍历map
    map<Info, int>::iterator it;
    for(it = m.begin(); it != m.end(); it++)
    {
        cout << (*it).second << " : ";
        cout << ((*it).first).name << " " << ((*it).first).score << endl;
    }
    return 0;
}
 
/*
10 : Bomi 80
30 : Peti 66.5
25 : Jack 60
*/
P51

猜你喜欢

转载自www.cnblogs.com/mjn1/p/10322338.html