map(树)干货归纳+用法示例

一.map简介

1.map特性

  1. map是STL的一个关联容器,它的底层结构是红黑树(unordered_map的底层结构是哈希表),其处理一对一数据的效率较高;
  2. map中的所有元素都是pair,pair的第一个元素称为关键字(key),第二个称为值(value);
  3. map中所有元素(pair)都会根据元素的键值(key)自动排序,不允许键值重复;
  4. 根据(key)值快速查找记录,查找的复杂度是O(log n);
    在这里插入图片描述

1.map头文件及构造对象

#include <map>  //注意,STL头文件没有扩展名.h
map<int, string> mapStudent;

1.map常用库函数

	begin()         //返回一个迭代器,指向map中的第一个键值对。
	end()           //返回一个迭代器,指向map中最后一个键值对的下一个位置。
	rbegin()        //返回一个指向map尾部的逆向迭代器
	rend()          //返回一个指向map头部的逆向迭代器
	equal_range()   //返回一个pair对象,其中包含两个迭代器,第一个迭代器指向第一个等于给定键的键值对,第二个迭代器指向最后一个等于给定键的键值对的下一个位置
	erase()         //根据键删除一个键值对。
	clear(//清空map中的所有键值对。
	empty()         //如果map为空则返回true
	count()         //返回指定元素出现的次数
	size()          //返回map中键值对的数量。
	max_size()      //返回可以容纳的最大元素个数
	find()          //查找一个键是否存在于map中,如果找到了,则返回该键对应的迭代器,否则返回map::end()。
	insert()        //插入元素
	key_comp()      //返回比较元素key的函数
	value_comp()    //返回比较元素value的函数
	lower_bound()   //返回一个迭代器,指向第一个不小于给定键的键值对。
	upper_bound()   //返回一个迭代器,指向第一个大于给定键的键值对。
	swap()          //交换两个map
	get_allocator() //返回map的配置器

二.map常用实例

1.map数据插入(insert)

#include "pch.h"
#include <iostream>
#include <string>
#include <map>  
using namespace std;

map<int, string> mapStudent;

//以键值对的方式,打印map中的所有数据
void print_map(map<int, string> a) {
	map<int, string>::iterator iter;
	for (iter = a.begin(); iter != a.end(); iter++) {
		cout << iter->first << ' ' << iter->second << endl;
	}
	cout << endl;
}

int main(){
	//法一:插入pair组数据
	mapStudent.insert(pair<int, string>(1, "student_one"));
	mapStudent.insert(pair<int, string>(2, "student_two"));
	mapStudent.insert(pair<int, string>(3, "student_three"));
	print_map(mapStudent);
	mapStudent.clear();

	//法二:插入value_type数据
	mapStudent.insert(map<int, string>::value_type(4, "student_four"));
	mapStudent.insert(map<int, string>::value_type(5, "student_five"));
	mapStudent.insert(map<int, string>::value_type(6, "student_six"));
	print_map(mapStudent);
	mapStudent.clear();

	//法三:用数组方式插入数据
	mapStudent[1] = "student_one";
	mapStudent[2] = "student_two";
	mapStudent[3] = "student_three";
	print_map(mapStudent);

	int nSize = mapStudent.size();
	cout << "map元素个数为:" << nSize << endl;;
}
  • 插入效果:

在这里插入图片描述

2.map数据的遍历(打印输出)

//法一:以迭代器的方式,遍历打印map中的所有数据对
void print_map(map<int, string> a) {
	map<int, string>::iterator iter;
	for (iter = a.begin(); iter != a.end(); iter++) {
		cout << iter->first << ' ' << iter->second << endl;
	}
	cout << endl;
}
//法二:以数组的方式,遍历打印map中的所有数据对(不推荐)
void print_map(map<int, string> a) {
	int n = a.size();
	for (int i = 1; i <= n; i++) {
		cout << i << " " << a[i] << endl;
	}
}

注:若删除过尾部数据(key靠后的),法二打印会出错,故不推荐。

3.由key查找对应的value

#include "pch.h"
#include <iostream>
#include <string>
#include <map>  
using namespace std;

map<int, string> mapStudent;

int main(){

	mapStudent.insert(pair<int, string>(1, "student_one"));
	mapStudent.insert(pair<int, string>(2, "student_two"));
	mapStudent.insert(pair<int, string>(3, "student_three"));

	map<int, string>::iterator iter;
	iter = mapStudent.find(1);      //find()内的参数是key
	if (iter != mapStudent.end())
		cout << "Find, the value is " << iter->second << endl;
	else
		cout << "Do not Find" << endl;
}
  • 查找结果:

在这里插入图片描述

4.其他操作

#include "pch.h"
#include <iostream>
#include <string>
#include <map>  
using namespace std;

//以迭代器的方式,遍历打印map中的所有数据对
void print_map(map<int, string> a) {
	map<int, string>::iterator iter;
	for (iter = a.begin(); iter != a.end(); iter++) {
		cout << iter->first << ' ' << iter->second << endl;
	}
	cout << endl;
}

map<int, string> mapStudent;

int main(){

	mapStudent.insert(pair<int, string>(1, "student_one"));
	mapStudent.insert(pair<int, string>(2, "student_two"));
	mapStudent.insert(pair<int, string>(3, "student_three"));
	mapStudent.insert(pair<int, string>(4, "student_four"));
	mapStudent.insert(pair<int, string>(5, "student_five"));
	mapStudent.insert(pair<int, string>(6, "student_six"));

	//找到key=1这个pair元素的迭代器
	map<int, string>::iterator iter;
	iter = mapStudent.find(1);

	//通过迭代器删除元素
	mapStudent.erase(iter);  

	//通过key删除元素
	mapStudent.erase(2);

	//通过count()快速判断map中是否有这个key
	int x=mapStudent.count(3);
	if (x != 0) cout << "存在key=3这个元素" << endl;

	print_map(mapStudent);
}
  • 效果展示

在这里插入图片描述

三.使用STL中的algorithm库对map进行操作

1.std::transform:

该函数将一个输入区间的每个元素应用于给定函数,并将结果存储在输出区间中。下面是一个使用std::transform函数将map中的值进行平方的示例:

#include <iostream>
#include <map>
#include <algorithm>
#include <iterator>

int main()
{
    std::map<int, int> myMap {{1, 2}, {2, 4}, {3, 6}, {4, 8}};
    std::map<int, int> newMap;

    std::transform(myMap.begin(), myMap.end(), std::inserter(newMap, newMap.begin()),
                   [](const std::pair<int, int>& p) { return std::make_pair(p.first, p.second * p.second); });

    for (const auto& p : newMap) {
        std::cout << p.first << " " << p.second << std::endl;
    }

    return 0;
}

输出:
1 4
2 16
3 36
4 64
该程序使用std::transform函数和一个lambda表达式,将myMap中的每个键值对都转换成一个新的键值对,其中新的键值对的键是原始值的键,而新的键值对的值是原始值的值的平方。然后,使用std::inserter函数将这些新的键值对插入到newMap中。

2.std::for_each:

该函数对输入区间中的每个元素应用给定函数。下面是一个使用std::for_each函数将map中的值打印出来的示例:

#include <iostream>
#include <map>
#include <algorithm>

void printValue(const std::pair<int, std::string>& p)
{
    std::cout << p.second << std::endl;
}

int main()
{
    std::map<int, std::string> myMap {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}};

    std::for_each(myMap.begin(), myMap.end(), printValue);

    return 0;
}

输出:
one
two
three
four
该程序使用std::for_each函数和一个自定义函数printValue,将myMap中的每个值打印出来。

3.std::sort:

该函数对输入区间中的元素进行排序。下面是一个使用std::sort函数将map按键排序的示例:

#include <iostream>
#include <map>
#include <algorithm>

int main()
{
    std::map<int, std::string> myMap {{4, "four"}, {1, "one"}, {3, "three"}, {2, "two"}};

    std::sort(myMap.begin(), myMap.end());

    for (const auto& p : myMap) {
        std::cout << p.first << " " << p.second << std::endl;
    }

    return 0;
}

输出:
1 one
2 two
3 three
4 four
该程序使用std::sort函数将myMap按键进行排序,然后使用一个for循环遍历已排序的map,并打印每个键值对的键和值

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiuZuqiang_3027

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值