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

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.xdnf.cn/news/10747.html

相关文章:

  • 【C/C++】template 入门到高阶简单大纲
  • rabbitMQ初入门
  • LangChain操作指南
  • 三、kafka消费的全流程
  • 6月2日day43打卡
  • 安全大模型的思考
  • 每日算法 -【Swift 算法】查找字符串数组中的最长公共前缀
  • 婚恋小程序直播系统框架搭建
  • VBA模拟进度条
  • 飞书常用功能(留档)
  • Dockerfile 使用多阶段构建(build 阶段 → release 阶段)后端配置
  • 从Java的JDK源码中学设计模式之装饰器模式
  • 2021 RoboCom 世界机器人开发者大赛-高职组(复赛)解题报告 | 珂学家
  • C#学习12——预处理
  • 当 AI 超越人类:从技术突破到文明拐点的 2025-2030 年全景展望
  • Manus AI与多语言手写识别的创新革命:从技术突破到行业赋能
  • 第2章_Excel_知识点笔记
  • 第十三章 Java基础-特殊处理
  • 【iOS】多线程基础
  • ArrayList和LinkedList(深入源码加扩展)
  • Day-15【选择与循环】选择结构-if语句
  • Q:知识库-文档的搜索框逻辑是怎样的?
  • 解决VS Code误报Java问题的终极方法
  • 深入理解 Java 环境变量:从原理到实战配置指南
  • LangChain系列之LangChain4j集成Spring Bot
  • AI“实体化”革命:具身智能如何重构体育、工业与未来生活
  • Android 中的 DataBinding 详解
  • 在图像分析算法部署中应对流行趋势的变化|文献速递-深度学习医疗AI最新文献
  • 大模型赋能:金融智能革命中的特征工程新纪元
  • 兼容老设备!EtherNet/IP转DeviceNet网关解决储能产线通讯难题