重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!
每一次学习都是为了追求智慧!
写在前面,本篇章主要介绍STL中常用容器map/multimap。
1.1 map基本概念
简介:
- map中所有元素都为键值对,即pair;
- pair中第一个元素为key(键),起到索引作用,第二个元素为value(值);
- map中所有元素都会跟进元素的key进行自动排序。默认升序。
本质:
map/multimap 都属于关联式容器,底层结构是用二叉树实现的。
优点:
可以根据key值,快速找到value值。
map和multimap区别:
- map不允许有重复key的元素;
- multimap允许有重复key的元素。
1.2 map构造和赋值
函数原型:
构造:
map<T1, T2> mp;
//map默认构造函数:map(const map &mp);
//拷贝构造函数
赋值:
map& operator=(const map &mp);
//重载等号操作符
示例:
#include <iostream>
#include <string>
#include<map>
using namespace std;
void printMap(const map<int, int>& m) {
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first
<< " value = " << it->second
<< endl;
}
cout << endl;
}
void test() {
// 创建map,insert插入数据,默认按key值升序排序
// 不运行插入重复key值
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
m.insert(pair<int, int>(4, 50));
printMap(m);
// 拷贝构造
map<int, int> m2(m);
printMap(m2);
// 赋值
map<int, int> m3;
m3 = m2;
printMap(m3);
}
int main() {
test();
system("pause");
return 0;
}
1.3 map大小和交换
函数原型:
size();
//返回容器中元素的数目empty();
//判断容器是否为空swap(st);
//交换两个集合容器
示例:
#include <iostream>
#include <string>
#include<map>
using namespace std;
void printMap(const map<int, int>& m) {
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first
<< " value = " << it->second
<< endl;
}
cout << endl;
}
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
printMap(m);
if (m.empty()) {
cout << " map is empty" << endl;
}
else {
cout << "map is not empty" << endl;
cout << "map size: " << m.size() << endl;
}
}
// 交换
void test2() {
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
cout << "map size: " << m.size() << endl;
printMap(m);
map<int, int> m2;
m2.insert(pair<int, int>(4, 40));
m2.insert(pair<int, int>(5, 50));
m2.insert(pair<int, int>(6, 60));
cout << "map size: " << m2.size() << endl;
printMap(m2);
m.swap(m2);
cout << "change:" << endl;
cout << "map size: " << m.size() << endl;
printMap(m);
cout << "map size: " << m2.size() << endl;
printMap(m2);
}
int main() {
test2();
system("pause");
return 0;
}
1.4 map插入和删除
函数原型:
- insert(elem); //在容器中插入元素。要插入对组
- clear(); //清除所有元素
- erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
- erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
- erase(key); //删除容器中值为key的元素。
示例:
#include <iostream>
#include <string>
#include<map>
using namespace std;
//insert(elem); //在容器中插入元素。
//clear(); //清除所有元素
//erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
//erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
//erase(key); //删除容器中值为key的元素。
void printMap(const map<int, int>& m) {
for (auto item : m) {
cout << " key = " << item.first
<< " value = " << item.second
<< " " << endl;
}
cout << endl;
}
void test() {
// map容器插入数据
map<int, int> m;
//第一种
m.insert(pair<int, int>(1, 10));
//第二种
m.insert(make_pair(2, 20));
//第三种
m.insert(map<int, int>::value_type(3, 30));
//第四种
m[4] = 40;
// 不建议用[]来插入数据
// [] 主要用来通过key来访问到value
//cout << m[4] << endl;
printMap(m);
//删除
m.erase(m.begin());
printMap(m);
m.erase(3);//按照key来删除
m.erase(10);//删除没有key的元素,不会报错
printMap(m);
//m.erase(m.begin(), m.end());// 利用迭代器来清空
m.clear();// 清空容器
printMap(m);
}
int main() {
test();
system("pause");
return 0;
}
1.5 map查找和统计
函数原型:
- find(key); 查找key是否存在。若存在,返回该键的元素的迭代器;若不存在,返回m.end();
- count(key); 统计key的元素个数。map只会有0或1,multimap有0和n。
示例:
#include <iostream>
#include <string>
#include<map>
using namespace std;
void test() {
map<int, int> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(3, 40));
map<int,int>::iterator it = m.find(3);
if (it !=m.end()) {
cout << " find! key = " << (*it).first
<< " value = " << (*it).second
<< endl;
}
else {
cout << "not find" << endl;
}
// 统计
// 由于map只能插入不可重复key值的元素,所以统计结果只能是 0 或 1
// multimap 可以统计出 0 或n
int n = m.count(3);
cout << "find key =3 times:" << n << endl;
n = m.count(5);
cout << "find key =5 times:" << n << endl;
}
int main() {
test();
system("pause");
return 0;
}
输出:
find! key = 3 value = 30
find key =3 times:1
find key =5 times:0
1.6 map容器排序
问题:
map容器默认排序是按照key从大到小排序,那如何做到改变排序规则?
方法:
利用仿函数,改变排序规则。
示例:
#include <iostream>
#include <string>
#include<map>
using namespace std;
class MyCompare {
public:
bool operator()(int v1,int v2)const {
return v1 > v2;
}
};
void printMap(const map<int, int, MyCompare>& m) {
for (map<int, int, MyCompare>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first
<< " value = " << it->second
<< endl;
}
cout << endl;
}
void test() {
//map 默认排序规则 从小到大
// 利用仿函数改变排序规则 从大到小
map<int, int, MyCompare>m;
m.insert(make_pair(3, 30));
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(4, 40));
printMap(m);
}
int main() {
test();
system("pause");
return 0;
}