C++ set数据插入、set数据查找、set数据删除、set数据统计、set排序规则、代码练习1、2

news2025/6/6 8:29:50

set数据插入,代码见下

#include<iostream>
#include<set>
#include<vector>

using namespace std;

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

int main() {
	// 树形结构插入的时间复杂度为 O(logn)
	// 直接插入值
	set<int> s;
	s.insert(4);
	s.insert(3);
	s.insert(8);

	// 迭代器插入
	vector<int> a = { 4, 5, 9 };
	s.insert(a.begin(), a.end());
	printSet(s); // 重复的值不会插入

	return 0;
}

结果见下,辅助理解:

3 4 5 8 9

set数据查找,代码见下:

#include<iostream>
#include<set>
#include<vector>

using namespace std;

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

int main() {
	set<int> s = { 8, 5, 9, 2, 1, 3 };

	set<int>::iterator it = s.find(3);
	if (it != s.end()) {
		cout << "find" << (*it) << endl;
	}
	it = s.find(10);
	if (it == s.end()) {
		cout << "can't find" << endl;
	}

	return 0;
}

set数据删除,代码见下:

#include<iostream>
#include<set>
#include<vector>

using namespace std;

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

int main() {
	set<int> s = { 8, 5, 9, 2, 4, 1, 3 };
	s.erase(2);
	printSet(s);

	set<int>::iterator rm = s.find(4);
	if (rm != s.end()) {
		s.erase(rm);
	}
	printSet(s);

	s = { 1, 2, 3, 4, 5 };
	set<int>::iterator rml = s.find(2); // 删除元素后,迭代器就失效了
	set<int>::iterator rmr = s.find(4);
	s.erase(rml, rmr);// 左闭右开区间 [ ) 不删除4
	printSet(s);




	return 0;
}

结果见下,辅助理解

1 3 4 5 8 9
1 3 5 8 9
1 4 5

set数据统计,代码见下:

#include<iostream>
#include<set>
#include<vector>

using namespace std;

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

void printmultiSet(const multiset<int>& s) {
	for (multiset<int>::const_iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	set<int> s = { 4, 5, 7, 9, 3 };
	for (int i = 0; i < 8; ++i) {
		cout << "元素:" << i << "的出现次数为:" << s.count(i) << endl;
	}
	multiset<int> ms = { 1, 1, 1, 2, 2, 3, 7, 7, 7 };
	for (int i = 0; i < 8; ++i) {
		cout << "元素:" << i << "的出现次数为:" << ms.count(i) << endl;
	}
	printmultiSet(ms);


	return 0;
}

结果见下,有助理解:

元素:0的出现次数为:0
元素:1的出现次数为:0
元素:2的出现次数为:0
元素:3的出现次数为:1
元素:4的出现次数为:1
元素:5的出现次数为:1
元素:6的出现次数为:0
元素:7的出现次数为:1
元素:0的出现次数为:0
元素:1的出现次数为:3
元素:2的出现次数为:2
元素:3的出现次数为:1
元素:4的出现次数为:0
元素:5的出现次数为:0
元素:6的出现次数为:0
元素:7的出现次数为:3
1 1 1 2 2 3 7 7 7

set排序规则,以下是属于结构体的情况,代码见下:

#include<iostream>
#include<set>
#include<vector>

using namespace std;

class CGAGA {
public:
	CGAGA() {
		_name = "";
		_priority = -1;
	}
	CGAGA(string name, int pri) : _name(name), _priority(pri) {}

	bool operator<(const CGAGA& other) const {
		return _priority < other._priority;
	}
	void print() const {
		cout << "(" << _priority << ")" << _name << endl;
	}
private:
	string _name;
	int _priority;
};

int main() {
	set<CGAGA> s;
	s.insert(CGAGA("C++算法零基础", 5));
	s.insert(CGAGA("C++面向对象", 4));
	s.insert(CGAGA("C++stl", 3));
	s.insert(CGAGA("C++项目实战", 2));
	s.insert(CGAGA("C++算法零基础2", 1));
	for (set<CGAGA>::iterator it = s.begin(); it != s.end(); it++) {
		(*it).print();
	}

	return 0;
}

结果见下,助于理解

(1)C++算法零基础2
(2)C++项目实战
(3)C++stl
(4)C++面向对象
(5)C++算法零基础

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2398215.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C# winform教程(二)

一、基础控件 常用的基础控件主要有按钮&#xff0c;文本&#xff0c;文本输入&#xff0c;组&#xff0c;进度条&#xff0c;等等。 基础控件 名称含义详细用法Button按钮Buttoncheckbox多选按钮Combobox下拉选择groupbox组控件label标签&#xff0c;显示文字panel控件集合&a…

【仿生机器人】刀剑神域计划——仿生机器人.亚丝娜

我在做仿生机器人头&#xff0c;硬件部分已经搭建完毕&#xff0c;包括头部和颈部&#xff0c;用的23个舵机驱动机器人做表情&#xff0c;也支持头部的旋转&#xff08;就是颈部的功能&#xff09;&#xff0c;安装了摄像头在眼睛中&#xff0c;还有麦克风接受周围环境声音&…

ARM架构推理Stable Diffusiond

代码仓库&#xff1a; https://github.com/siutin/stable-diffusion-webui-docker.git Docker容器地址&#xff1a; https://hub.docker.com/r/siutin/stable-diffusion-webui-docker/tags git clone https://github.com/siutin/stable-diffusion-webui-docker.git cd stabl…

仓颉项目调试配置与多文件场景下的问题解析

1. 调试配置指南 在 VS Code 中配置好仓颉开发工具链后&#xff0c;只需按下 F5 或 Fn F5 即可启动调试。 在 CodeArts IDE for Cangjie 中&#xff0c;需先通过右上角的 编辑配置 -> 新增配置项 -> 选择 Cangjie (cjdb) Debug -> 选择 launch 模式 -> 点击 确认…

MySQL 8.0 OCP 英文题库解析(十)

Oracle 为庆祝 MySQL 30 周年&#xff0c;截止到 2025.07.31 之前。所有人均可以免费考取原价245美元的MySQL OCP 认证。 从今天开始&#xff0c;将英文题库免费公布出来&#xff0c;并进行解析&#xff0c;帮助大家在一个月之内轻松通过OCP认证。 本期公布试题81~90 试题81:…

Python Pytest

1.Pytest用例发现规则 1.1 模块名(python文件)名必须以 test_ 开头或 _test 结尾&#xff0c;如 test_case&#xff0c;case_test&#xff0c;下划线都不能少 1.2 模块不能放在 . 开头的隐藏目录或者叫 venv的目录下&#xff0c;virtual environment&#xff0c;叫venv1都可以…

如何轻松删除 Android 上的文件(3 种方法)

Android 手机是非常强大的设备&#xff0c;可让我们存储大量的个人数据&#xff0c;从照片和视频到应用程序和文档。然而&#xff0c;随着时间的推移&#xff0c;您的设备可能会因不再需要的文件而变得混乱。删除这些文件有助于释放空间并提高性能。在本指南中&#xff0c;我们…

[特殊字符] Unity UI 性能优化终极指南 — ScrollRect篇

ScrollRect ManualScrollRect API 我参考了官方最新文档&#xff08;基于UGUI 3.0包&#xff09;&#xff0c;加上实际性能测试经验&#xff0c;直接给你梳理&#xff1a; &#x1f3af; Unity UI 性能优化终极指南 — ScrollRect篇 &#x1f9e9; 什么是 ScrollRect&#xff…

自适应流量调度用于遥操作:面向时间敏感网络的通信与控制协同优化框架

英文标题&#xff1a;Adaptive Flow Scheduling for Teleoperation: A Communication and Control Co-Optimization Framework over Time-Sensitive Networks 中文标题&#xff1a;自适应流量调度用于遥操作&#xff1a;面向时间敏感网络的通信与控制协同优化框架 作者信息 …

阿里云服务器-解决宝塔登录不成功

出现问题&#xff1a; This site can’t be reached XX.XX.XXX.XXX took too long to respond. Try: Checking the connection Checking the proxy and the firewall Running Windows Network Diagnostics ERR_CONNECTION_TIMED_OUT 可能是端口未开放 原因&#xff1a;服务器…

graphviz, dot, Error: lost rA sA edge; 独立的模块

1) 有向图dot文件 digraph R { node [shaperecord]; { ranksame rA sA tA } { ranksame uB vB wB } rA -> sA; sA -> vB; t -> rA; uB -> vB; wB -> u; wB -> tA; } 2&#xff09;出现报警信息 Warning: flat edge between adjacent …

Axure-元件流程图

Axure-02 线框图元件使用 目标 元件基本介绍 基础元件的使用 表单型元件的使用 菜单与表格元件的使用 案例&#xff1a;个人简历表 元件基本介绍 概述 在Axure RP中&#xff0c;元件是构建原型图的基础模块。 将元件从元件库里拖拽到画布中&#xff0c;即可添加元件到你…

Python爬虫解析动态网页:从渲染到数据提取

一、动态网页与静态网页的区别 在开始之前&#xff0c;我们需要理解动态网页与静态网页的区别。静态网页的内容在服务器端是固定的&#xff0c;每次请求都会返回相同的结果&#xff0c;通常以HTML文件的形式存储。而动态网页则不同&#xff0c;其内容是通过JavaScript在客户端…

LLMs之MCP:如何使用 Gradio 构建 MCP 服务器

LLMs之MCP&#xff1a;如何使用 Gradio 构建 MCP 服务器 导读&#xff1a;本文详细介绍了如何使用Gradio构建MCP服务器&#xff0c;包括前提条件、构建方法、关键特性和相关资源。通过一个简单的字母计数示例&#xff0c;演示了如何将Gradio应用转换为LLM可以使用的工具。Gradi…

VBA模拟进度条

在上一章中我跟大家介绍了ProgressBar控件的使用方法&#xff0c;但由于该控件无法在64位版本的Office中运行&#xff0c;为此我们可以采用Lable控件来模拟进度条的变化&#xff0c;以解决在64位版本的Office中无进度条控件的问题。 一、设计思路 添加两个重叠的Lable标签控件…

MySQL强化关键_019_索引优化

目 录 一、最左前缀原则 1.完全使用索引 2.部分使用索引 3.不使用索引 4.效率折损 &#xff08;1&#xff09;使用范围查找 &#xff08;2&#xff09;索引断开 二、索引失效场景 1. 索引列参与运算 2.索引列模糊查询以“%”开始 3.索引列是字符串类型&#xff0c;查…

关于list集合排序的常见方法

目录 1、list.sort() 2、Collections.sort() 3、Stream.sorted() 4、进阶排序技巧 4.1 空值安全处理 4.2 多字段组合排序 4.3. 逆序 5、性能优化建议 5.1 并行流加速 5.2 原地排序 6、最佳实践 7、注意事项 前言 Java中对于集合的排序操作&#xff0c;分别为list.s…

不动产登记区块链系统(Vue3 + Go + Gin + Hyperledger Fabric)

好久没有介绍过新项目的制作了&#xff0c;之前做的一直都是Fisco Bcos的项目&#xff0c;没有介绍过Hyperledger Fabric的项目&#xff0c;这次来给大家分享下。 系统概述 不动产登记与交易平台是一个基于Hyperledger Fabric的综合性管理系统&#xff0c;旨在实现不动产登记…

从 GPT 的发展看大模型的演进

这是一个技术爆炸的时代。一起来看看 GPT 诞生后&#xff0c;与BERT 的角逐。 BERT 和 GPT 是基于 Transformer 模型架构的两种不同类型的预训练语言模型。它们之间的角逐可以从 Transformer 的编码解码结构角度来分析。 BERT&#xff08;Bidirectional Encoder Representatio…

【Qt】构建目录设置

问题 ProjectRoot/├── src/ # 源代码│ ├── project1│ └── project2├── build/ # 构建目录│ ├── build-PCIeDemoApp-Desktop_Qt_5_9_7_MSVC2015_64bit-Debug/│ └── build-PCIeDemoApp-Desktop_Qt_5_9_7_MSVC2015_64bit-Rele…