【C++】map/multimap容器

1.map基本概念

2.map构造和赋值

cpp 复制代码
#include <iostream>
using namespace std;

//map容器 构造和赋值
#include<map>

//遍历输出map容器
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 test01()
{
	//创建map容器
	map<int, int>m;

	//按照key值自动排序
	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);

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

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

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

3.map大小和交换

cpp 复制代码
#include <iostream>
using namespace std;

//map容器 大小和交换
#include<map>

void printMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << (*it).first << "    value = " << it->second << endl;
	}
	cout << endl;
}

//大小
void test01()
{
	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));

	if (m.empty())
	{
		cout << "m为空!" << endl;
	}
	else
	{
		cout << "size of m : " << m.size() << endl;
	}
}

//交换
void test02()
{
	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));

	map<int, int>m2;
	m2.insert(pair<int, int>(4, 100));
	m2.insert(pair<int, int>(6, 300));
	m2.insert(pair<int, int>(5, 200));

	cout << "交换前:" << endl;
	printMap(m);
	printMap(m2);

	//m与m2交换
	m.swap(m2);
	cout << "交换后:" << endl;
	printMap(m);
	printMap(m2);
}

int main()
{
	test01();
	cout << "-------------" << endl << endl;
	test02();

	//**************************************
	system("pause");
	return 0;
} 

4.map插入和删除


cpp 复制代码
#include <iostream>
using namespace std;

//map容器 插入和删除
#include<map>

void printMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << (*it).first << "  value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	//创建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;

	//[]不建议插入, []其用途 可以利用可以访问到value
	//cout << m[5] <<endl; //输出了" key = 5   value = 0 "
	printMap(m);

	//删除
	m.erase(m.begin());
	printMap(m);

	m.erase(3); //按照key删除
	printMap(m);

	//清空
	m.erase(m.begin(), m.end());
	m.clear();
	printMap(m);
}

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

5.map查找和统计


cpp 复制代码
#include <iostream>
using namespace std;

//map容器 查找和统计
#include<map>
void test01()
{
	//查找
	map<int, int>m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(2, 40)); // 不能插入重复元素(以key为标准)


	map<int, int>::iterator pos = m.find(3);

	if (pos != m.end())
	{
		cout << "找到了该元素  key = " << (*pos).first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "no find this element" << endl;
	}

	//统计
	//map不允许插入重复元素, count统计而言 结果只能是0或1
	//multimap的count统计可能大于1
	int num = m.count(2);
	cout << "num = " << num << endl;
}

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

6.map容器排序


cpp 复制代码
#include <iostream>
using namespace std;

//map容器 排序
#include<map>

class MyCompare
{
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2; // 降序
	}
};

void test01()
{
	//默认从小到大排序
	//利用仿函数实现从小到大排序
	map<int, int, MyCompare> m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(2, 20));

	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "   value = " << it->second << endl;
	}
}

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 
相关推荐
敲上瘾7 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3
人间乄惊鸿客13 小时前
c++ 右值
c++·算法
ShineWinsu15 小时前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
瑶池酒剑仙16 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
郝学胜-神的一滴2 天前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光2 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
青少儿编程课堂2 天前
蓝桥杯真题解析:手链配色计数与波利亚定理(全国青少年信息素养大赛与电子学会软件编程考级)
c++·python·算法·bfs·信息学竞赛
艾莉丝努力练剑2 天前
【AI大模型接入SDK】项目总结
java·开发语言·前端·c++·人工智能·学习·架构
旖旎夜光2 天前
面试题 01.02: 判定是否互为字符重排 (哈希表) —— 题解
c++·笔记·学习·算法·哈希表
白小飞不会飞2 天前
【AUTOSAR】CP SOME/IP-TP
c++·嵌入式硬件·网络协议·车载系统·autosar