4.map
一.map简介
1.map特性
- map是STL的一个关联容器,它的底层结构是红黑树(unordered_map的底层结构是哈希表),其处理一对一数据的效率较高;
- map中的所有元素都是pair,pair的第一个元素称为关键字(key),第二个称为值(value);
- map中所有元素(pair)都会根据元素的键值(key)自动排序,不允许键值重复;
- 根据(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,并打印每个键值对的键和值