STL——map/multimap容器

目录

1.map基本概念

2.map构造和赋值

3.map大小和交换

4.map插入和删除

5.map查找和统计

6.map容器排序


1.map基本概念

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

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

优点:可以根据key值快速找到value值。

map和multimap的区别:

  • map不允许容器中有重复的key值
  • multimap允许容器中有重复的key值 

2.map构造和赋值

函数原型:

  • map<T1, T2> mp; ——//map默认构造函数。
  • map(const map &mp); ——//拷贝构造函数
  • map& operator=(const map &mp); ——//重载等号操作符
#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;
}
//map容器——构造和赋值
void test01()
{
	map<int, int>m1;
	m1.insert(pair<int, int>(1, 100));
	m1.insert(pair<int, int>(2, 200));
	m1.insert(pair<int, int>(3, 300));
	m1.insert(pair<int, int>(4, 400));
	printMap(m1);
	//拷贝构造
	map<int, int>m2(m1);
	printMap(m2);
	//赋值
	map<int, int>m3;
	m3 = m2;
	printMap(m3);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.map大小和交换

函数原型:

  • size(); ——//返回容器中元素的数目
  • empty(); ——//判断容器是否为空
  • swap(st); ——//交换两个集合容器
#include<iostream>
using namespace std;
#include<map>
//map容器——大小和交换
//大小
void test01()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 100));
	m.insert(pair<int, int>(2, 200));
	m.insert(pair<int, int>(3, 300));
	if (m.empty())
	{
		cout << "m为空" << endl;
	}
	else
	{
		cout << "m不为空" << endl;
		cout << "m的大小为:" << m.size() << endl;
	}
}
//交换
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 test02()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 100));
	m.insert(pair<int, int>(2, 200));
	m.insert(pair<int, int>(3, 300));
	map<int, int>m2;
	m2.insert(pair<int, int>(4, 400));
	m2.insert(pair<int, int>(5, 500));
	m2.insert(pair<int, int>(6, 600));
	cout << "交换前:" << endl;
	printMap(m);
	printMap(m2);
	m.swap(m2);
	cout << "交换后:" << endl;
	printMap(m);
	printMap(m2);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

4.map插入和删除

函数原型:

  • insert(elem); ——//在容器中插入元素。
  • clear(); ——//清除所有元素
  • erase(pos); ——//删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg, end); ——//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • erase(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>m;
	//插入
	//第一种
	m.insert(pair<int, int>(1, 10));
	//第二种
	m.insert(make_pair(2, 20));
	//第三种
	m[3] = 30;
	//第四种
	m.insert(map<int, int>::value_type(4, 40));
	//[]不建议插入用途,可以利用key访问到value
	cout << m[4] << endl;
	printMap(m);
	//删除
	m.erase(m.begin());
	printMap(m);
	m.erase(4);//按照key进行删除
	printMap(m);
	//清空
	//m.erase(m.begin(), m.end());
	m.clear();
	printMap(m);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5.map查找和统计

函数原型:

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • count(key); //统计key的元素个数
#include<iostream>
using namespace std;
#include<map>
//map容器——查找和统计
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));
	m.insert(pair<int, int>(4, 40));
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "找到了 key = " << pos->first << " value = " << (*pos).second << endl;
	}
	else
	{
		cout << "没有找到。" << endl;
	}
	multimap<int, int>m2;
	m2.insert(pair<int, int>(1, 10));
	m2.insert(pair<int, int>(2, 20));
	m2.insert(pair<int, int>(3, 30));
	m2.insert(pair<int, int>(3, 40));
	int num = m2.count(3);
	cout << "num = " << num << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

6.map容器排序

技术点:利用仿函数改变排序规则。

#include<iostream>
using namespace std;
#include<map>
//map容器——排序
//仿函数
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));
	m.insert(make_pair(6, 60));
	for (map<int, int, myCompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << " value = " << it->second << endl;
	}
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

注:

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

相关推荐

  1. python -- 容器

    2024-01-02 09:52:03       42 阅读
  2. 【Spring】容器

    2024-01-02 09:52:03       36 阅读
  3. set容器

    2024-01-02 09:52:03       21 阅读
  4. vector容器

    2024-01-02 09:52:03       22 阅读
  5. Python—容器

    2024-01-02 09:52:03       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 09:52:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 09:52:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 09:52:03       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 09:52:03       20 阅读

热门阅读

  1. 如何配置TensorRT版的Katago

    2024-01-02 09:52:03       44 阅读
  2. 世岩清上:跨年倒计时如何通过全息投影呈现

    2024-01-02 09:52:03       34 阅读
  3. 《Linux详解:深入探讨计算机基础》

    2024-01-02 09:52:03       34 阅读
  4. Spring Boot实战:深入理解@Service与@Mapper注解

    2024-01-02 09:52:03       37 阅读
  5. ARM AArch64的虚拟化(virtualization)详解(下)

    2024-01-02 09:52:03       41 阅读
  6. http基本格式

    2024-01-02 09:52:03       40 阅读
  7. 位运算trick

    2024-01-02 09:52:03       43 阅读
  8. ChatGPT的基本原理?

    2024-01-02 09:52:03       40 阅读
  9. GitHub Copilot的快速入门

    2024-01-02 09:52:03       40 阅读