STL---map/multimap容器

时间:2021-08-25
本文章向大家介绍STL---map/multimap容器,主要包括STL---map/multimap容器使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

9. map/multimap容器

9.1 map基本概念

简介:

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

本质:

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

优点:

  • 可以根据key值快速找到value值
    map和multimap区别:
  • map不允许容器中有重复的 key值元素
  • multimap允许容器中有重复的key值元素

9.2 map构造和赋值

功能描述:

  • 对map容器进行构造和赋值操作
    函数原型:
    构造:
  • map<T1,T2>mp;//map默认构造函数
  • map(const map &mp);//拷贝构造函数
    赋值:
  • map& operator=(const map &mp);//重载等号操作符

示例:

#include<iostream>
using namespace std;
#include<map>
//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;
	}
}
void test01()
{
	map<int, int>m;//创建map容器
	//插入的是对组
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(5, 50));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(3, 30));
	cout << "m=" << endl;
	printMap(m);//打印输出
	//拷贝构造
	map<int, int>m2(m);
	cout << "m2=" << endl;
	printMap(m2);
	//赋值
	map<int, int>m3;
	m3 = m2;
	cout << "m3=" << endl;
	printMap(m3);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

m=
key= 1 value= 10
key= 2 value= 20
key= 3 value= 30
key= 4 value= 40
key= 5 value= 50
m2=
key= 1 value= 10
key= 2 value= 20
key= 3 value= 30
key= 4 value= 40
key= 5 value= 50
m3=
key= 1 value= 10
key= 2 value= 20
key= 3 value= 30
key= 4 value= 40
key= 5 value= 50
请按任意键继续. . .

总结:map中所有元素都是成对出现的,插入数据时候要使用对组


9.3 map大小和交换

功能描述:

  • 统计map容器大小以及交换map容器
    函数原型:
  • size();
  • empty();
  • swap(st);

示例:

#include<iostream>
using namespace std;
#include<map>
///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;
	}
}
void test01()
{
	map<int, int>m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(3, 30));
	if (m1.empty())
	{
		cout << "m1为空" << endl;
	}
	else
	{
		cout << "m1不为空,大小为:";
		cout << m1.size() << endl;
	}
	map<int, int>m2;
	m2.insert(pair<int, int>(4,40));
	m2.insert(pair<int, int>(5, 50));
	m2.insert(pair<int, int>(6, 60));
	cout << "交换前:" << endl;
	printMap(m1);
	cout << endl;
	printMap(m2);
	cout << "交换后:" << endl;
	m1.swap(m2);
	printMap(m1);
	cout << endl;
	printMap(m2);

}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

m1不为空,大小为:3
交换前:
key=:1  value=:10
key=:2  value=:20
key=:3  value=:30

key=:4  value=:40
key=:5  value=:50
key=:6  value=:60
交换后:
key=:4  value=:40
key=:5  value=:50
key=:6  value=:60

key=:1  value=:10
key=:2  value=:20
key=:3  value=:30
请按任意键继续. . .

9.4 map插入和删除

功能描述:

  • map容器进行插入数据和删除数据

函数原型:

  • insert(elem);
  • clear();//
  • erase(pos);
  • erase(beg,end);
  • erase(key);
    示例:
#include<iostream>
using namespace std;
#include<map>
//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;
	}
}
void test01()
{
	map<int, int>m1;
	//插入,第一种
	m1.insert(pair<int, int>(1, 10));
	//第二种
	m1.insert(make_pair(2, 20));
	//第三种
	m1.insert(map<int, int>::value_type(3, 30));
	//第四种 []不建议插入,用途:可以利用key访问到value
	m1[4] = 40;
	cout << m1[4] << endl;
	printMap(m1);
	//删除
	cout << endl;
	m1.erase(m1.begin());
	printMap(m1);
	//
	cout << endl;
	m1.erase(3);//按照key删除
	printMap(m1);
	//清空操作
	m1.erase(m1.begin(), m1.end());// = m1.clear();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

40
key=1 value=10
key=2 value=20
key=3 value=30
key=4 value=40

key=2 value=20
key=3 value=30
key=4 value=40

key=2 value=20
key=4 value=40
请按任意键继续. . .

总结:

  • 插入---insert
  • 删除---erase
  • 清空---clear

9.5 map查找和统计

功能描述:

  • 对map容器进行查找数据以及统计数据
    函数原型:
  • find(key);//查找key元素是否存在,返回该键元素的迭代器;若不存在,返回set.end();
  • count(key);//统计key的元素个数

示例:

#include<iostream>
using namespace std;
#include<map>
//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>m1;
	m1.insert(make_pair(1, 10));
	m1.insert(pair<int, int>(2, 20));
	m1[3] = 30;
	//m1.find(3);返回的是一个迭代器,需要创建一个迭代器接收
	map<int, int>::iterator pos = m1.find(3);
	if (pos != m1.end())
	{
		cout << "查到了元素key = " << (*pos).first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	//统计
	//map不允许插入重复的key,对于count统计而言,结果要么是0,要么是1
	int num=m1.count(3);
	cout << "num=" << num << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

查到了元素key = 3 value = 30
num=1
请按任意键继续. . .

总结:

  • 查找---find(返回的是迭代器)
  • 统计---count(对于map,结果是0或1)

9.6 map容器排序

学习目标:

  • map容器默认排序规则为按照key值进行从大到小的操作,掌握如何改变排序规则
    主要技术点:
  • 利用仿函数,可以改变排序规则

示例:

#include<iostream>
using namespace std;
#include<map>
//map容器排序
class myCompare
{
public:
	bool operator()(int v1, int v2)const//仿函数
	{
		return v1 > v2;//降序排序
		//return v1<v2  升序排序
	}
};
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>m1;
	m1.insert(make_pair(1, 10));
	m1.insert(pair<int, int>(2, 20));
	m1[3] = 30;
	m1.insert(make_pair(4, 40));
	m1.insert(make_pair(5, 50));
	printMap(m1);
	//修改排序规则
	map<int, int,myCompare>m2;
	m2.insert(make_pair(1, 10));
	m2.insert(pair<int, int>(2, 20));
	m2[3] = 30;
	m2.insert(make_pair(4, 40));
	m2.insert(make_pair(5, 50));
	for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++)
	{
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 50

key = 5 value = 50
key = 4 value = 40
key = 3 value = 30
key = 2 value = 20
key = 1 value = 10
请按任意键继续. . .

总结:

  • 利用仿函数可以指定map容器的排序规则
  • 对于自定义数据类型,map必须要指定排序规则,同set容器

原文地址:https://www.cnblogs.com/QYepoch/p/15183987.html