💃🏼 本人简介:男
👶🏼 年龄:18
✍每日一句:【道固远,笃行可至;事虽巨,坚为必成】
文章目录
- 1. 基本概念
 - 2. 构造赋值
 - ① 函数原型
 - ② 代码展示
 - ③ 测试结果
 
- 3. 大小交换
 - ① 函数原型
 - ② 代码展示
 - ③ 测试结果
 
- 4. 插入删除
 - ① 函数原型
 - ② 代码展示
 - ③ 测试结果
 
- 5. 查找统计
 - ① 函数原型
 - ② 代码展示
 - ③ 测试结果
 
- 6. 排序
 - ① 函数原型
 - ② 代码展示
 - ③ 测试结果
 
- 最后,感谢大家支持u (^ _ ^)
 
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")
                

















