map/multimap(学习笔记)

本文深入讲解了C++标准库中的map容器,包括其基本概念、构造函数、大小与交换、插入与删除、查找与统计以及如何自定义排序规则等关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.基本概念
简介:

  • map中所有元素都是pair
  • pair中的第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据键值自动排序

本质:

  • map/mapmultimap属于关联式容器,底层结构是二叉树实现

优点:

  • 可以根据key值快速找到value值

map/mapmultimap区别:

  • map不允许容器有重复的key值元素(但value值可以相同)
  • mulyimap允许容器有重复的key值元素

2.构造函数

#include<iostream>
using namespace std;
#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;
	//自动按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();
	system("pause");
	return 0;
}

3.大小与交换

功能:统计map容器的大小和交换map容器

#include<iostream>
using namespace std;
#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(pair<int, int>(3, 30));
	m.insert(pair<int, int>(2, 20));

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

	//交换
	map<int, int>m2;
	m2.insert(pair<int, int>(4, 99));
	m2.insert(pair<int, int>(5, 66));
	m2.insert(pair<int, int>(6, 88));
	m.swap(m2);
	printMap(m);
	printMap(m2);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.插入与删除

#include<iostream>
using namespace std;
#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(3, 30));
	//第三种
	m.insert(map<int,int>::value_type(2,20));
	//第四种(不建议)
	m[4] = 40;
	//第四种利用key值访问value值,会出现cout << m[5];使不存在的value=0;
	printMap(m);

	//删除
	//第一种
	m.erase(m.begin());
	printMap(m);
	//第二种--只能通过key值删除,不能通过value值
	m.erase(3);
	printMap(m);
	//第三种
	m.erase(m.begin(), m.end());
	printMap(m);
	//清空
	//m.clear();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5.查找与统计

#include<iostream>
using namespace std;
#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(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	//查找
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "查找成功!key=" << pos->first <<" value="<< (*pos).second << endl;
	}
	else
	{
		cout << "查找失败!" << endl;
	}

	//统计
	int num = m.count(3);
	cout << "出现次数:" << num << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

6.容器排序

#include<iostream>
using namespace std;
#include<map>
class myCompare
{
public:
	bool operator()(int m1, int m2)const
	{
		return m1 > m2;
	}
};
void printMap(map<int, int, myCompare>& m)
{
	for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key=" << (*it).first << " value=" << it->second << endl;
	}cout << endl;
}
void test01()
{
	map<int, int, myCompare>m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(5, 50));
	m.insert(make_pair(3, 30));
	//改变默认排序规则 改为从从大到小
	//利用仿函数重写一个类型
	printMap(m);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值