当前位置: 首页 > news >正文

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

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);// 左闭右开区间 [ ) 不删除4printSet(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.lqws.cn/news/109441.html

相关文章:

  • 6月2日day43打卡
  • 【Python进阶】元类编程
  • 基于c++面向对象的设计(下)
  • 亚马逊Woot提报常见问题第一弹
  • C#基础:使用线程池执行并行任务
  • 蓝绿部署解析
  • 【leetcode-两数之和】
  • 笔记本电脑开机无线网卡自动禁用问题
  • 开源模型应用落地-OpenAI Agents SDK-集成Qwen3-8B(一)
  • 【北邮 操作系统】第十三章 I/O系统
  • 推荐算法八股
  • git clone报错:SSL certificate problem: unable to get local issuer certificate
  • 金融中的线性优化:投资组合分配与求解器 - Part 2
  • 【大模型】ChatGLM训练框架
  • R1-Searcher++新突破!强化学习如何赋能大模型动态知识获取?
  • 产品更新丨谷云科技ETLCloud 3.9.3 版本发布
  • Qiskit:量子计算模拟器
  • 深入理解汇编语言中的顺序与分支结构
  • 19-项目部署(Linux)
  • 新德通科技:以创新驱动光通信一体化发展,赋能全球智能互联
  • CAMEL-AI开源自动化任务执行助手OWL一键整合包下载
  • 依赖注入-@Resource和@Autowired
  • Java并发编程实战 Day 5:线程池原理与使用
  • EMQX 社区版单机和集群部署
  • HCIP(BGP综合实验)
  • 学习STC51单片机26(芯片为STC89C52RCRC)
  • 通过阿里云 DashScope API 调用通义千问
  • 51c大模型~合集134
  • Redis缓存-数据淘汰策略
  • 6.RV1126-OPENCV 形态学基础膨胀及腐蚀