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可以插入重复的值

相关推荐
我唔知啊20 分钟前
OC底层原理二:OC对象的分类(实例对象、类对象、元类对象)
ios·objective-c
Digitally2 小时前
如何将信息从 iPhone 同步到Mac(完整步骤和示意图)
macos·ios·iphone
守城小轩10 天前
Chromium 136 编译指南 macOS篇:编译优化技巧(六)
macos·策略模式
Areslee10 天前
一种通用跨平台实现SEH的解决方案
linux·macos·内核·跨平台·seh
麦兜*10 天前
【node】Mac m1 安装nvm 和node
java·前端·vue.js·chrome·macos·vue·nvm
2401_8885670011 天前
Mac电脑-媒体文件格式转换-Permute
macos·mac·媒体·格式转换
开开心心就好11 天前
高效批量转换Word到PDF的方法
javascript·安全·智能手机·pdf·word·objective-c·lisp
2501_9205525612 天前
Mac电脑-音视频剪辑编辑-Final Cut Pro X(fcpx)
macos·音视频·mac
Digitally12 天前
如何轻松地将联系人从 iPhone 转移到 iPhone?
ios·cocoa·iphone
zh4men912 天前
Mac Parallels Desktop Kali 2025 代理设置
服务器·网络·macos·kali·pdesktop