[C++ STL] map使用详解

一、set介绍:

Map由红黑树实现,其元素都是“键值/实值”所形成的一个对组(key/value pairs)。每个元素有一个键,是排序准则的基础。每一个键只能出现一次,不允许重复。

Map主要用于资料一对一映射(one-to-one)的情况,map内部自建一颗红黑树(平衡二叉树中的一种),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。比如一个班级中,每个学生的学号跟他的姓名就存在着一对一映射的关系。


二、用法

1、头文件

#include <map>
//map属于std命名域的,因此需要通过命名限定,例如using std::map;

2、定义及初始化

map<int, string> a; //定义一个int类型的映射a
//map<int, string> a(10); //error,未定义这种构造函数
//map<int, string> a(10, 1); //error,未定义这种构造函数
map<int, string> b(a); //定义并用映射a初始化映射b
//map<int, string> b(a.begin(), a.end());  //error,未定义这种构造函数

3、基本操作

(1) 容量函数

  • 容器大小: mp.size();
  • 容器最大容量: mp.max_size();
  • 更改容器大小: mp.resize();
  • 容器判空: mp.empty();
  • 查找键key的元素个数: mp.count(key);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<int,string> mp;
    mp.insert({ 1, "张三" });
    mp.insert({ 2, "李四" });
    mp.insert(pair<int, string>{ 3, "隔壁老王" });

    cout << "元素大小: " << mp.size() << endl;
    cout << "元素最大容量: " << mp.max_size() << endl;
    cout << "键2的元素个数: " << mp.count(2) << endl;
    if (mp.empty())
        cout << "元素为空" << endl;

    return 0;
}

/*
元素大小: 3
元素最大容量: 89478485
键2的元素个数: 1
*/

(2) 增加函数

  • 在容器中插入元素:mp.insert(const T& x);
  • 任意位置插入一个元素: mp.insert(iterator it, const T& x);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<int,string> mp;
    //在容器中插入元素
    mp.insert({ 1, "张三" });
    mp.insert({ 2, "李四" });
    //任意位置插入一个元素
    map<int, string>::iterator it = mp.begin();
    mp.insert(it, pair<int, string>{ 3, "隔壁老王" }); //会自动排序

    for (it = mp.begin(); it != mp.end(); it++)
        cout << it->first << " " << it->second << endl;
    cout << endl;

    return 0;
}

/*
1 张三
2 李四
3 隔壁老王
*/

(3) 删除函数

  • 删除键值为keyValue的元素: mp.pop_back(const T& keyValue);
  • 删除迭代器所指的元素: mp.erase(iterator it);
  • 删除区间[first,last]之间的所有元素: mp.erase(iterator first, iterator last);
  • 清空所有元素: mp.clear();
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<int,string> mp;
    //在容器中插入元素
    mp.insert({ 1, "张三" });
    mp.insert({ 2, "李四" });
    mp.insert({ 4, "王五" });
    mp.insert({ 5, "小明" });
    //任意位置插入一个元素
    mp.insert(mp.begin(), pair<int, string>{ 3, "隔壁老王" }); //会自动排序

    //删除键值为keyValue的元素
    mp.erase(2);
    //删除迭代器所指的元素
    mp.erase(mp.begin());
    //删除区间[first,last]之间的所有元素
    mp.erase(mp.begin(), ++mp.begin());

    //display map
    map<int, string>::iterator it = mp.begin();
    for (it = mp.begin(); it != mp.end(); it++)
        cout << it->first << " " << it->second << endl;

    //清空容器内的所有元素
    mp.clear();

    //display map
    cout << "mp:";
    for (it = mp.begin(); it != mp.end(); it++)
        cout << it->first << " " << it->second << endl;
    cout << endl;

    return 0;
}

/*
4 王五
5 小明
mp:
*/

(4) 迭代器

  • 开始迭代器指针:mp.begin();
  • 末尾迭代器指针:mp.end(); //指向最后一个元素的下一个位置
  • 指向常量的开始迭代器指针: mp.cbegin(); //意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。
  • 指向常量的末尾迭代器指针: mp.cend();
  • 反向迭代器指针,指向最后一个元素: mp.rbegin();
  • 反向迭代器指针,指向第一个元素的前一个元素: mp.rend();
  • 返回最后一个key<=keyElem元素的迭代器: mp.lower_bound(keyElem);
  • 返回第一个key>keyElem元素的迭代器: mp.upper_bound(keyElem);
  • 返回容器中key与keyElem相等的上下限的两个迭代器,这两个迭代器被放在对组(pair)中: mp.equal_range(keyElem);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<int,string> mp;
    //在容器中插入元素
    mp[1] = "张三";
    mp[2] = "李四";
    mp[3] = "隔壁老王";

    cout << "*(mp.begin()): " << mp.begin()->first << endl;
    cout << "*(mp.end()): " << (--mp.end())->first << endl;
    cout << "*(mp.cbegin()): " << mp.cbegin()->first << endl;
    cout << "*(mp.cend()): " << (--mp.cend())->first << endl;
    cout << "*(mp.rbegin()): " << mp.rbegin()->first << endl;
    cout << "*(mp.rend()): " << (--mp.rend())->first << endl;
    cout << "*(mp.lower_bound(2)): " << mp.lower_bound(2)->first << endl;
    cout << "*(mp.upper_bound(2)): " << mp.upper_bound(2)->first << endl;
    pair<map<int, string>::iterator, map<int, string>::iterator> t_pair = mp.equal_range(2);
    cout << "*(t_pair.first): " << t_pair.first->first << endl;
    cout << "*(t_pair.second): " << t_pair.second->first << endl;
    cout << endl;

    return 0;
}

/*
*(mp.begin()): 1
*(mp.end()): 3
*(mp.cbegin()): 1
*(mp.cend()): 3
*(mp.rbegin()): 3
*(mp.rend()): 1
*(mp.lower_bound(2)): 2
*(mp.upper_bound(2)): 3
*(t_pair.first): 2
*(t_pair.second): 3
*/

(5) 访问函数

  • 查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end(): mp.find(key);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<int,string> mp;
    //在容器中插入元素
    mp[1] = "张三";
    mp[2] = "李四";
    mp[3] = "隔壁老王";

    //通过find(key)查找键值
    cout << "find(key)查找键: " << mp.find(1)->first << endl;
    cout << "find(key)查找值: " << mp.find(2)->second << endl;

    return 0;
}

/*
find(key)查找键: 1
find(key)查找值: 李四
*/

(6) 其他函数

  • 交换两个同类型容器的元素: swap(map&, map&); 或 mp.swap(map&);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<int,string> mp1;
    //在容器中插入元素
    mp1[1] = "张三";
    mp1[2] = "李四";
    mp1[3] = "隔壁老王";

    map<int, string> mp2;
    //在容器中插入元素
    mp2[1] = "tom";
    mp2[2] = "jerry";
    mp2[3] = "mariy";

    //交换两个容器的元素
    //swap(mp1,mp2); //ok
    mp2.swap(mp1);

    //通过iterator遍历mp1
    map<int, string>::iterator it;
    for (it = mp1.begin(); it != mp1.end(); it++)
        cout << it->second << " ";
    cout << endl;

    return 0;
}

/*
tom jerry mariy
*/

(7) 算法

  • 遍历元素
map<int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++)
    cout << it->second << endl;


三、总结

可以看到,map 与set的用法基本一致,只有以下几处不同:

  • map 可以像数组那样插入元素,而 set 不行。

猜你喜欢

转载自www.cnblogs.com/linuxAndMcu/p/10261263.html
今日推荐