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

相关推荐
aini_lovee13 小时前
寻找 MAC 协议的 MATLAB 仿真
开发语言·macos·matlab
咨询qq 8762239651 天前
多源动态最优潮流分布式鲁棒优化探索
cocoa
2501_915106321 天前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915918411 天前
iOS 性能监控 运行时指标与系统行为的多工具协同方案
android·macos·ios·小程序·uni-app·cocoa·iphone
TheNextByte11 天前
适用于Windows和Mac电脑的Android文件传输工具
windows·macos·电脑
00后程序员张1 天前
IPA 混淆技术全解,从成品包结构出发的 iOS 应用安全实践与工具组合
android·安全·ios·小程序·uni-app·cocoa·iphone
Orange裴1 天前
Kali linux2025.3 安装nessus(Mac M4芯片)
linux·运维·macos·kali linux
通域1 天前
MacOS Ventura EasyConnect 安装提示 “正在等待其他安装完成“
macos
Digitally1 天前
5种将iPhone同步到Mac/MacBook的方法
macos·ios·iphone
Digitally1 天前
7种在iPhone和Mac之间传输文件的最佳方法
macos·ios·iphone