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);// 左闭右开区间 [ ) 不删除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++算法零基础

代码练习一,对应力扣,不间断子数组,代码见下

class Solution {
public:
    long long continuousSubarrays(vector<int>& nums) {
        multiset<int> mst;
        int i = 0, j = -1;
        int n = nums.size();
        long long ans = 0;
        while(j++ < n-1){
            mst.insert(nums[j]);
            int min = *mst.begin();
            int max = *mst.rbegin();
            while(max - min > 2){
                multiset<int>::iterator it = mst.find(nums[i]);
                mst.erase(it);
                i++;
                min = *mst.begin();
                max = *mst.rbegin();
            }
            ans += j - i + 1;

        }
        return ans;
    }
};

代码2 对应力扣,最高频率的ID,代码见下

class Solution {
public:
    vector<long long> mostFrequentIDs(vector<int>& nums, vector<int>& freq) {
        multiset<long long> ms;
        multiset<long long>::iterator it;
        long long cnt[100001] = {0};
        vector<long long> ans(nums.size(), 0);
        for(int i = 0; i < nums.size(); ++i){
            int x = nums[i];
            long long &c = cnt[x];
            it = ms.find(c);
            if(it != ms.end()){
                ms.erase(it);
            }
            c += freq[i];
            ms.insert(c);
            ans[i] = *ms.rbegin();
        }
        return ans;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值