2021-8-19 C++提升学习 -> 3.9、map容器

本文详细介绍了C++标准模板库(STL)中map容器的基本操作,包括通过insert插入元素、使用拷贝构造和赋值操作创建新的map、检查map的空状态和大小、交换两个map的内容,以及通过erase进行删除操作。此外,还展示了如何使用find进行查找和count进行元素统计。最后,讨论了自定义排序规则以改变map的默认排序方式。
摘要由CSDN通过智能技术生成

3.9.2、赋值和构造

#include <iostream>
#include <string>
#include <map>
using namespace std;

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));
	m.insert(pair<int, int>(4, 40));

	printMap(m);

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

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

int main()
{

	test01();
	system("pause");
}

3.9.3、大小和交换

#include <iostream>
#include <string>
#include <map>
using namespace std;

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));
	m.insert(pair<int, int>(4, 40));

	if (m.empty())
	{
		cout << "m为空" << endl;
	}
	else
	{
		cout << "m不为空" << endl;
		cout << "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));
	m.insert(pair<int, int>(4, 40));

	map<int, int>m2;

	m2.insert(pair<int, int>(1, 100));
	m2.insert(pair<int, int>(3, 300));
	m2.insert(pair<int, int>(2, 200));
	m2.insert(pair<int, int>(4, 400));

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

	m.swap(m2);

	cout << "交换后:" << endl;
	printMap(m);
	printMap(m2);
}
int main()
{
	test02();
	test01();
	system("pause");
}

3.9.4、插入和删除

m.insert(pair<int, int>(1, 10));

  m.insert(make_pair(2, 20));

m.insert(map<int, int>::value_type(3, 30));
//[]不建议插入,建议用第二种方法 make_pair
m[4] = 40;

#include <iostream>
#include <string>
#include <map>
using namespace std;

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;
		cout << endl;
	}
	cout << endl;
	
}

void test01()
{
	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));
	//[]不建议插入,建议用第二种方法 make_pair
	m[4] = 40;

	cout << m[5] << endl;
	printMap(m);

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

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

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

	m.clear();
	printMap(m);
}

int main()
{

	test01();
	system("pause");
}

3.9.5、查找和统计

查找——find(返回的是迭代器)

统计——count(对于map,结果为0或者1)

#include <iostream>
#include <string>
#include <map>
using namespace std;

void test01()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

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

	if (pos != m.end())
	{
		cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

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

int main()
{

	test01();
	system("pause");
}

3.9.6、容器排序

利用仿函数可以指定map函数的排序规则

对于自定义数据类型,map必须要指定排序规则,同set容器

#include <iostream>
#include <string>
#include <map>
using namespace std;

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


};

void test01()
{
	map<int, int,MyCompare>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(5, 50));

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

int main()
{

	test01();
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值