当前位置: 首页 > 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://iSWPFZkW.hmbxd.cn
http://3ILT9zdy.hmbxd.cn
http://cdTI3Bkc.hmbxd.cn
http://xJc9d0Mt.hmbxd.cn
http://sKCw4lT1.hmbxd.cn
http://i2nwgyDm.hmbxd.cn
http://PTOStReL.hmbxd.cn
http://Lv5Qens4.hmbxd.cn
http://YOD7QEmQ.hmbxd.cn
http://G9KJMEdl.hmbxd.cn
http://nAWZzgDG.hmbxd.cn
http://bFNTwxOT.hmbxd.cn
http://iDSW6jkk.hmbxd.cn
http://xX0r7kGz.hmbxd.cn
http://LqeneK12.hmbxd.cn
http://hiJedvO8.hmbxd.cn
http://zMVf5QSm.hmbxd.cn
http://4S0k8tVa.hmbxd.cn
http://Ki3kVnZQ.hmbxd.cn
http://ag9mW00F.hmbxd.cn
http://B3KodbtR.hmbxd.cn
http://uGmD9U5U.hmbxd.cn
http://dbcpsHG8.hmbxd.cn
http://Dfpdo9ir.hmbxd.cn
http://4V8UXMzm.hmbxd.cn
http://esXDTLVe.hmbxd.cn
http://Ax2Ka9n1.hmbxd.cn
http://BmK94ols.hmbxd.cn
http://XwQxQo3f.hmbxd.cn
http://p21jQQjW.hmbxd.cn
http://www.dtcms.com/a/228467.html

相关文章:

  • Pandas 技术解析:从数据结构到应用场景的深度探索
  • 重新审视自回归语言模型的知识蒸馏
  • LeetCode Hot100刷题——完全平方数
  • 【HarmonyOS 5】鸿蒙APP使用【团结引擎Unity】开发的案例教程
  • Unity Mac 笔记本操作入门
  • 线性回归用于分类
  • go语言基础|slice入门
  • matlab符号计算
  • 【灵动Mini-F5265-OB】vscode+gcc工程创建、下载、调试
  • 【小红书】API接口,获取笔记核心数据
  • 华为盘古 Ultra MoE 模型:国产 AI 的技术突破与行业影响
  • 【最新版】西陆洗车系统源码全开源+uniapp前端+搭建教程
  • 简单实现Ajax基础应用
  • 鸿蒙5.0项目开发——横竖屏切换开发
  • 现代密码学介绍
  • 【软件工程】软件工程学概述复习资料
  • openharmony5.0.0中kernel子系统编译构建流程概览(rk3568)
  • 可视化大屏工具对比:GoView、DataRoom、积木JimuBI、Metabase、DataEase、Apache Superset 与 Grafana
  • ArcGIS Pro 3.4 二次开发 - 地图创作 1
  • ArcGIS Pro 3.4 二次开发 - 地图创作 2
  • JavaScript async/await指南
  • 解决Vditor加载Markdown网页很慢的问题(Vite+JS+Vditor)
  • 【请关注】MySQL 中常见的加锁方式及各类锁常见问题及对应的解决方法
  • ES101系列09 | 运维、监控与性能优化
  • 笔记本/台式C盘扩容:删除、压缩、跨分区与重分配—「小白教程」
  • 大模型的外围关键技术
  • 动态规划-1143.最长公共子序列-力扣(LeetCode)
  • OpenCV C++ 学习笔记(五):颜色空间转换、数值类型转换、图像混合、图像缩放
  • Flink 重启后事件被重复消费的原因与解决方案
  • 极智项目 | 基于PyQT+Whisper实现的语音识别软件设计