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

发布于:2025-06-04 ⋅ 阅读:(21) ⋅ 点赞:(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() {
	// 树形结构插入的时间复杂度为 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++算法零基础


网站公告

今日签到

点亮在社区的每一天
去签到