Day19 C++STL入门基础知识十一——map、multimap容器 构造赋值、大小交换、插入删除、查找统计、排序【全面深度剖析+例题代码展示】

💃🏼 本人简介:男
👶🏼 年龄:18
✍每日一句:【道固远,笃行可至;事虽巨,坚为必成】

1. 基本概念

  • 所有元素都是两个两个出现的pair
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值),通过key值找到value值
  • 按键值自动排序
  • multimap可以插入重复的key值

2. 构造赋值

① 函数原型

  • map<T1, T2> m; 默认构造
  • map(const map &m); 拷贝构造

② 代码展示

#include<iostream>
#include<stdio.h>
#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 text01() {
	//默认构造
	map<int, int> m;
	m.insert(pair<int, int>(3, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(4, 30));
	m.insert(pair<int, int>(1, 40));
	m.insert(pair<int, int>(1, 50));
	printMap(m);	//自动按照key值由小到大排序

	//拷贝构造
	map<int, int> m2(m);
	printMap(m2);

	//赋值操作
	map<int, int> m3;
	m3 = m2;
	printMap(m3);
}

int main() {
	text01();
	return 0;
}

③ 测试结果

在这里插入图片描述

3. 大小交换

① 函数原型

  • size(); 返回容器元素数目
  • empty(); 判空
  • swap(st); 交换两个集合容器

② 代码展示

#include<iostream>
#include<stdio.h>
#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;
	//cout << "------------------------------------------------------" << endl;
}

//大小、交换
void text02() {
	map<int, int> m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(2, 15));
	m1.insert(pair<int, int>(3, 25));
	m1.insert(pair<int, int>(4, 30));

	cout << "m1容器大小为: " << m1.size() << endl;
	if (m1.empty()) cout << "m容器为空" << endl;
	else cout << "m1容器不为空" << endl;

	map<int, int> m2;
	m2.insert(pair<int, int>(5, 40));
	m2.insert(pair<int, int>(4, 50));
	m2.insert(pair<int, int>(8, 60));
	m2.insert(pair<int, int>(6, 70));
	cout << "交换前: " << endl;
	cout << "m1为: " << endl;  printMap(m1); cout << "m2为: " << endl; printMap(m2);
	m1.swap(m2);
	cout << "交换后: " << endl;
	cout << "m1为: " << endl; printMap(m1); cout << "m2为: " << endl;  printMap(m2);
}
int main(){
	text02();
	return 0;
}

③ 测试结果

在这里插入图片描述

4. 插入删除

① 函数原型

  • insert(elem); 插入元素
  • clear(); 清空
  • erase(pos); 删除迭代器所指的元素,并返回下一个迭代器
  • erase(beg, end); 删除区间[beg, end)的所有元素,返回下一个元素的迭代器
  • erase(key); 删除容器中值为key的元素

② 代码展示

#include<iostream>
#include<stdio.h>
#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;
	//cout << "------------------------------------------------------" << endl;
}

void text03() {
	map<int, int> m;
	//插入
	//第一种pair()
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(4, 40));
	//第二种make_pair
	m.insert(make_pair(5, 50));
	//第三种::value_type
	m.insert(map<int, int>::value_type(6, 60));
	//第四种[]
	m[7] = 70;
	printMap(m);

	//删除
	m.erase(3);  //删除为key元素的值
	printMap(m);

	m.erase(++ m.begin() , m.end());
	printMap(m);

	m.clear();
	printMap(m);
}
int main(){
	text03();
	return 0;
}

③ 测试结果

5. 查找统计

① 函数原型

  • find(key); 查找key是否存在
    • 若存在,则返回元素的迭代器
    • 不存在,返回map.end();
  • count(key); 统计key的个数

② 代码展示

#include<iostream>
#include<stdio.h>
#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;
	//cout << "------------------------------------------------------" << endl;
}

void text04() {
	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(4, 40));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(4, 40));

	//查找
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end()) cout << "查找到了" << endl;
	else cout << "未查找到" << endl;
	
	//统计
	int cnt = m.count(4);
	cout <<"key为4的个数为:" << cnt << endl;  //不允许插入重复key
}

③ 测试结果

在这里插入图片描述

6. 排序

① 函数原型

② 代码展示

#include<iostream>
#include<stdio.h>
#include<map>
using namespace std;

class cmp {
public:
	bool operator()(int x, int y)const{
		return x > y;
	}//从大到小排
};

void text05() {
	map<int, int, cmp> m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(2, 20));
	
	for (map<int, int, cmp>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
}

③ 测试结果

在这里插入图片描述

最后,感谢大家支持u (^ _ ^)

如果感觉这篇文章对你有帮助的话,不妨三连支持下,十分感谢(✪ω✪)。

printf("点个赞吧*^*");
cout << "收藏一下叭o_o";
System.out.println("评论一下吧^_^");
print("关注一下叭0-0")
评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卫冕711

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

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

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

打赏作者

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

抵扣说明:

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

余额充值