set容器

set基本概念 构造和赋值

所有元素会在插入时自动排序

cpp 复制代码
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
const int N = 2e5 + 10, M = 1e3 + 10;
using namespace std;
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << ' ';
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	s1.insert(30);
	printSet(s1);//所有元素自动被排序,不允许插入重复的值
	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//赋值
	set<int>s3;
	s3 = s2;
	printSet(s3);
}
int main()
{
	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	test01();
	return 0;
}

set大小和交换

cpp 复制代码
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
const int N = 2e5 + 10, M = 1e3 + 10;
using namespace std;
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << ' ';
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	printSet(s1);
	//判断是否为空
	if (s1.empty())
	{
		cout << "空" << endl;
	}
	else cout << "not empty" << endl;
	cout << "s1大小" << s1.size();
}
void test02()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set<int>s2;
	s2.insert(100);
	s2.insert(200);
	s2.insert(300);
	s2.insert(400);
	cout << "交换前"<<endl;
	printSet(s1);
	printSet(s2);
	s1.swap(s2);
	cout << "交换后"<<endl;
	printSet(s1);
	printSet(s2);

}
int main()
{
	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	test02();
	return 0;
}

set插入和删除

cpp 复制代码
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
const int N = 2e5 + 10, M = 1e3 + 10;
using namespace std;
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << ' ';
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	printSet(s1);
	//删除
	s1.erase(s1.begin());
	printSet(s1);
	//删除重载版本
	s1.erase(30);
	printSet(s1);

	//清空
	s1.erase(s1.begin(), s1.end());//==s1.clear()
	printSet(s1);
}
void test02()
{
	
}
int main()
{
	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	test01();
	return 0;
}

set查找和统计

cpp 复制代码
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
const int N = 2e5 + 10, M = 1e3 + 10;
using namespace std;
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << ' ';
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	//printSet(s1);
	set<int>::iterator pos = s1.find(100);
	if (pos != s1.end())
	{
		cout << "ok:" << *pos<<endl;
	}
	else
	{
		cout << "no" << endl;
	}
}
void test02()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(30);
	s1.insert(30);

	int num = s1.count(30);//统计30的个数 对于set而言 统计结果要么是0要么是1
	cout << "num:" << num;
}
int main()
{
	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	test02();
	return 0;
}

set和multiset的区别

multiset可以插入重复的值

相关推荐
阿四4 小时前
mac升级系统到macOS26后触控板滑动异常,原因竟然是这个!
macos
mortimer5 小时前
从 Python+venv+pip 迁移到 uv 全过程 及 处理 torch + cuda 的跨平台指南
pytorch·python·macos
2501_915921438 小时前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
Mac技巧大咖1 天前
苹果电脑如何维护优化系统:让mac重获新生的系统维护优化全攻略
macos·苹果电脑系统优化
氷泠1 天前
解决MacOS上CLion调试的时候容器的值显示为0的问题
macos·调试·lldb·clion
他们都不看好你,偏偏你最不争气1 天前
【iOS】KVC总结
macos·ios·objective-c·cocoa·kvc
1024小神2 天前
关于在ios系统中签名并安装ipa文件的五种方法,PakePlus打包的ipa文件可以看看
macos·ios·cocoa
漂亮_大男孩2 天前
conda|如何通过命令行在mac上下载conda
macos·conda
止观止2 天前
VS Code 二次开发:跨平台图标定制全攻略
linux·windows·vscode·macos
小瓶盖_tl2 天前
在Mac上安装CocoaPods问题处理
macos·xcode·cocoapods