IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++——STL(map/ multimap容器) -> 正文阅读

[C++知识库]C++——STL(map/ multimap容器)

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

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

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

map和multimap区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素

构造:
map<T1, T2> mp; //map默认构造函数:
map(const map &mp); //拷贝构造函数

赋值:
map& operator=(const map &mp); //重载等号操作符

#include <iostream>
#include <map>

using namespace std;

void printmap(const map<int, int>&m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
		cout << "键值: " << it->first << "  值: " << it->second << endl;
	}
	cout << endl;
}

void test()
{
	//默认构造
	map<int, int>m1;

	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 20));
	m1.insert(pair<int, int>(2, 30));
	m1.insert(pair<int, int>(4, 40));
	m1.insert(pair<int, int>(5, 50));

	printmap(m1);

	//拷贝构造
	map<int, int>m2(m1);
	printmap(m2);
	

	map<int, int>m3;
	m3 = m2;
	printmap(m3);

}

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

map大小和交换:

size(); //返回容器中元素的数目
empty(); //判断容器是否为空
swap(st); //交换两个集合容器

map插入和删除:

insert(elem); //在容器中插入元素。
clear(); //清除所有元素
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(key); //删除容器中值为key的元素。

map插入的多种方式:

#include <iostream>
#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;
	}

}

void test()
{
	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));

	//第四种: (如果打印一个使用[]表示但没有创建的数时,会自动创建为0;但是使用[]可以访问元素)
	m1[4] = 40;
	cout << m1[5] << endl;

	printmap(m1);

}
int main()
{

	test();
	system("pause");
	return 0;

}

在这里插入图片描述
map查找和统计

find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key); //统计key的元素个数(对于map由于不允许有重复元素,所以find只能是0或1;但是multimap 统计可能不为0和1)

使用find加一个判断条件,如果find返回的是m.end(),就说明没有找到,如果不为m.end()说明找到

map容器排序
利用仿函数

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

class compare {

public:
	bool operator() (int data1, int data2)
	{
		return data1 > data2;
	}
};



void test01()
{
	map<int, int,compare>m1;
	m1.insert(make_pair(1, 10));
	m1.insert(make_pair(3, 30));
	m1.insert(make_pair(2, 20));
	m1.insert(make_pair(5, 50));
	m1.insert(make_pair(4, 40));
	cout << "降序排序" << endl;
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
		cout << "key: " << it->first << "  value: " << it->second << endl;
	}

}

void test02()
{
	map<int, int>m1;
	m1.insert(make_pair(1, 10));
	m1.insert(make_pair(3, 30));
	m1.insert(make_pair(2, 20));
	m1.insert(make_pair(5, 50));
	m1.insert(make_pair(4, 40));
	cout << "默认排序" << endl;
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
		cout << "key: " << it->first << "  value: " << it->second << endl;
	}

}

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

}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章           查看所有文章
加:2021-08-02 20:46:10  更:2021-08-02 20:46:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 14:55:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码