set容器、map容器

2023-10-26

set/ multiset 容器

set基本概念

简介:

  • 所有元素都会在插入时自动被排序

本质:

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

set和multiset区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

set构造和赋值

构造:

  • set st; //默认构造函数:
  • set(const set &st); //拷贝构造函数

赋值:

  • set& operator=(const set &st); //重载等号操作符
#include <set>

void printSet(set<int> & s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//构造和赋值
void test01()
{
	set<int> s1;
	
	//插入数据只有insert方式
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//赋值
	set<int>s3;
	s3 = s2;
	printSet(s3);
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

  • set容器插入数据时用insert
  • set容器插入数据的数据会自动排序

set大小和交换

函数原型:

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

void printSet(set<int> & s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//大小
void test01()
{

	set<int> s1;
	
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

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

}

//交换
void test02()
{
	set<int> s1;

	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

	set<int> s2;

	s2.insert(100);
	s2.insert(300);
	s2.insert(200);
	s2.insert(400);

	cout << "交换前" << endl;
	printSet(s1);
	printSet(s2);
	cout << endl;

	cout << "交换后" << endl;
	s1.swap(s2);
	printSet(s1);
	printSet(s2);
}

int main() {

	//test01();

	test02();

	system("pause");

	return 0;
}

总结:

  • 统计大小 — size
  • 判断是否为空 — empty
  • 交换容器 — swap

set插入和删除

函数原型:

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

void printSet(set<int> & s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//插入和删除
void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

	s1.erase(30);
	printSet(s1);

	//清空
	//s1.erase(s1.begin(), s1.end());
	s1.clear();
	printSet(s1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

插入 — insert
删除 — erase
清空 — clear

set查找和统计

函数原型:

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • count(key); //统计key的元素个数
#include <set>

//查找和统计
void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	
	//查找
	set<int>::iterator pos = s1.find(30);

	if (pos != s1.end())
	{
		cout << "找到了元素 : " << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	//统计
	int num = s1.count(30);
	cout << "num = " << num << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

查找 — find (返回的是迭代器)
统计 — count (对于set,结果为0或者1)

set和multiset区别

区别:

  • set不可以插入重复数据,而multiset可以
  • set插入数据的同时会返回插入结果,表示插入是否成功
  • multiset不会检测数据,因此可以插入重复数据
#include <set>

//set和multiset区别
void test01()
{
	set<int> s;
	pair<set<int>::iterator, bool>  ret = s.insert(10);
	if (ret.second) {
		cout << "第一次插入成功!" << endl;
	}
	else {
		cout << "第一次插入失败!" << endl;
	}

	ret = s.insert(10);
	if (ret.second) {
		cout << "第二次插入成功!" << endl;
	}
	else {
		cout << "第二次插入失败!" << endl;
	}
    
	//multiset
	multiset<int> ms;
	ms.insert(10);
	ms.insert(10);

	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

如果不允许插入重复数据可以利用set
如果需要插入重复数据利用multiset

pair对组创建

功能描述:

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式:

  • pair<type, type> p ( value1, value2 );
  • pair<type, type> p = make_pair( value1, value2 );
#include <string>

//对组创建
void test01()
{
	pair<string, int> p(string("Tom"), 20);
	cout << "姓名: " <<  p.first << " 年龄: " << p.second << endl;

	pair<string, int> p2 = make_pair("Jerry", 10);
	cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

set容器排序

主要技术点:

  • 利用仿函数,可以改变排序规则

示例一 set存放内置数据类型

#include <set>

class MyCompare 
{
public:
	bool operator()(int v1, int v2) const
	 {
		return v1 > v2;
	}
};
void test01() 
{    
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(50);

	//默认从小到大
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	//指定排序规则
	set<int,MyCompare> s2;
	s2.insert(10);
	s2.insert(40);
	s2.insert(20);
	s2.insert(30);
	s2.insert(50);

	for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:利用仿函数可以指定set容器的排序规则

示例二 set存放自定义数据类型

#include <set>
#include <string>

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;

};
class comparePerson
{
public:
	bool operator()(const Person& p1, const Person &p2)
	{
		//按照年龄进行排序  降序
		return p1.m_Age > p2.m_Age;
	}
};

void test01()
{
	set<Person, comparePerson> s;

	Person p1("刘备", 23);
	Person p2("关羽", 27);
	Person p3("张飞", 25);
	Person p4("赵云", 21);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);

	for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
	}
}
int main() {

	test01();

	system("pause");

	return 0;
}

总结:

对于自定义数据类型,set必须指定排序规则才可以插入数据

map/ multimap容器

map基本概念

简介:

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

本质:

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

优点:

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

map和multimap区别:

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

map构造和赋值

函数原型:

构造:

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

赋值:

  • map& operator=(const map &mp); //重载等号操作符
#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(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	printMap(m);

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

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

int main() {

	test01();

	system("pause");

	return 0;
}

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

map大小和交换

函数原型:

  • size(); //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st); //交换两个集合容器
#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(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	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>(2, 20));
	m.insert(pair<int, int>(3, 30));

	map<int, int>m2;
	m2.insert(pair<int, int>(4, 100));
	m2.insert(pair<int, int>(5, 200));
	m2.insert(pair<int, int>(6, 300));

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

	cout << "交换后" << endl;
	m.swap(m2);
	printMap(m);
	printMap(m2);
}

int main() {

	test01();

	test02();

	system("pause");

	return 0;
}

总结:

统计大小 — size
判断是否为空 — empty
交换容器 — swap

map插入和删除

函数原型:

  • insert(elem); //在容器中插入元素。
  • clear(); //清除所有元素
  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • erase(key); //删除容器中值为key的元素。
#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(pair<int, int>(1, 10));
	//第二种插入方式(推荐使用)
	m.insert(make_pair(2, 20));
	//第三种插入方式
	m.insert(map<int, int>::value_type(3, 30));
	//第四种插入方式 不建议使用
	m[4] = 40; 
	printMap(m);
	
	//不建议使用[]插入,但可以利用key访问到value
	cout<<m[4]<<endl;
	
	//删除
	m.erase(m.begin());
	printMap(m);

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

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

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

插入 — insert
删除 — erase
清空 — clear

map查找和统计

函数原型:

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • count(key); //统计key的元素个数
#include <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));

	//查找
	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 = " << num << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

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

map容器排序

主要技术点:

  • 利用仿函数,可以改变排序规则
#include <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));

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

	test01();

	system("pause");

	return 0;
}

总结:

  • 利用仿函数可以指定map容器的排序规则
  • 对于自定义数据类型,map必须要指定排序规则,同set容器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

set容器、map容器 的相关文章

随机推荐

  • 数据挖掘的种种-节课准备

    数据挖掘 Data Mining DM 又称数据库中的知识发现 Knowledge Discover in Database KDD 数据挖掘概述 数据挖掘 Data Mining DM 又称数据库中的知识发现 Knowledge Disc
  • C# InvokeRequired和Invoke

    一 简介 WinForm 关于InvokeRequired与Invoke Jlins的博客 CSDN博客 invokerequired c 是禁止夸线程直接访问控件 InvokeRequired是为了解决这个问题而产生的 当一个控件的Inv
  • cpu与gpu实现矩阵相乘对比

    1 完成矩阵相乘的并行程序的实现 要求 实现2个矩阵 1024 1024 的相乘 数据类型设置为float 1 使用CPU计算 include
  • 开源的一些基础介绍

    国内 淘宝 百度 南航 网易等 国外 新浪 搜狐 facebook ebay google等 成功后的企业也在不断为开源添加新能量 如 taobao和google等 因为他们不但被开源的魅力深深吸引住同时也愿意通过开源提升自我 现在更多的企
  • Wrappers.<实体>lambdaQuery的使用

    文章目录 MP 配置 Service CURD接口 Mapper CURD接口 insert delete update select 条件构造器 LambdaUpdateWrapper Wrappers lt 实体 gt lambdaUp
  • 机器人教育是一种科学的探究方式

    创新是推动经济社会发展的核心驱动力 当前 我国已经深刻认识到世界新科技革命带来的机遇和挑战 以高度的历史责任感 强烈的忧患意识和宽广的世界眼光 把创新作为推动经济社会发展的驱动力量 机器人技术的进步将会对科学与技术的发展产生重要影响 只有开
  • 算法(C)(两数之和)

    题目 两数之和 题目描述 给定一个整数数组 nums 和一个整数目标值 target 请你在该数组中找出 和为目标值 target 的那 两个 整数 并返回它们的数组下标 你可以假设每种输入只会对应一个答案 但是 数组中同一个元素在答案里不
  • JSON使用的一些总结

    http sx666 blogspot com 2007 11 json html JSON JavaScript Object Notation 是一种轻量级的数据交换格式 它采用完全独立于语言的文本格式 可以用来在客户端和服务器端传输数
  • innerText和innerHTML区别

    innerText和innerHTML区别 尽管DOM带来了动态修改文档的能力 但对开发人员来说 这还不够 IE4 0为所有的元素引入了两个特性 以更方便的进行文档操作 这两个特性是innerText和innerHTML 其中innerTe
  • Oracle:重复数据去重,只取其中一条(最新时间/其他字段排序规则)数据

    一 问题 一个会话id代表一个聊天室 返回该聊天室最新的一条数据显示在会话列表 二 解决思路 使用row number over 分组排序功能 来解决该问题 1 语法格式 row number over partition by 分组列 o
  • TMOD、SCON、PCON寄存器的配置

    TMOD控制寄存器 TMOD是定时器 计数器模式控制寄存器 它是一个逐位定义的8为寄存器 但只能使用字节寻址 其各位是 由上图我们就可以看出 这个寄存器控制了两个定时器 计数器 寄存器的高四位控制定时器1 低四位控制定时器0 GATE 门控
  • 数据分析毕业设计 二手房数据爬取与分析可视化系统 -python

    1 前言 这两年开始毕业设计和毕业答辩的要求和难度不断提升 传统的毕设题目缺少创新和亮点 往往达不到毕业答辩的要求 这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求 为了大家能够顺利以及最少的精力通过毕设 学长分享优质毕业设计项
  • Air700E开发板

    文章目录 基础资料 概述 主要功能 外设分布 PinOut 管脚定义 管脚功能说明 固件升级 正常开机模式 下载模式 IO 电平选择 基础资料 Air700E文档中心 概述 EVB Air700E 开发板是合宙通信推出的基于 Air700E
  • 去除VsCode代码前面的小点点

    去除VsCode代码前面的小点点 去除图片中的点 步骤 File gt Preferences gt Setting 搜索RenderWhitespace 将Text Editor下的Editor Render Whitespace改为no
  • peewee-async使用描述

    1 peewee async是一个为peewee ORM 提供由asyncio支持的异步io库 在单独使用peewee连接池连接时 同时使用到了async和await协程 这样操作会阻塞整个进程 因为tornado是单进程 必须数据库也使用
  • 数据库的简介与类型 #CSDN博文精选# #IT技术# #数据库#

    大家好 小C将继续与你们见面 带来精选的CSDN博文 又到周一啦 上周的系统化学习专栏已经结束 我们总共一起学习了20篇文章 这周将开启全新专栏 放假不停学 全栈工程师养成记 在这里 你将收获 将系统化学习理论运用于实践 系统学习IT技术
  • 高通 AR Unity 虚拟按钮

    1 虚拟按钮是图像上的目标 用户可以在现实世界中触摸 以触发一个动作的 热点 现有的图像目标的一个实例的VirtualButton预制拖动到场景中添加虚拟按键 平移和缩放按钮 以匹配所需的位置 并给它一个名字 虚拟的按钮添加这样写入到con
  • 计算机视觉概述

    关注公众号 CV算法恩仇录 本文介绍了计算机视觉的主要任务及应用 全文大约 3500 字 阅读时间 10 分钟 人们或许没有意识到自己的视觉系统是如此的强大 婴儿在出生几个小时后能识别出母亲的容貌 在大雾的天气 学生看见朦胧的身体形态 能辨
  • v-viewer 插件图片点击放大预览的几种使用方法

    官网git地址 https github com mirari v viewer 最终效果如下 ps 按钮样式都是可以根据自己需求调整的 第一种使用方法 支持UMD用法 建议把v viewer相关的js和css文件下载到本地引用 可以到上面
  • set容器、map容器

    set multiset 容器 set基本概念 简介 所有元素都会在插入时自动被排序 本质 set multiset属于关联式容器 底层结构是用二叉树实现 set和multiset区别 set不允许容器中有重复的元素 multiset允许容