STL set 容器

本文详细介绍了C++ STL中set容器的基本操作,包括构造赋值、大小和交换、插入删除、查找统计、set与multiset的区别,以及自定义数据类型排序。示例代码展示了如何使用set进行元素操作,并通过pair对组创建和操作元素。此外,还讲解了如何为自定义数据类型设置排序规则。
摘要由CSDN通过智能技术生成

1.set容器

1)构造赋值

#include <iostream>
using namespace std;
#include <set>

void printset(set<int>&s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{ 
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(5);
	printset(s1);

	set<int>s2(s1);
	set<int>s3;
	s3 = s1;
}

int main()
{
	test01();
}

2)大小和交换

#include <iostream>
using namespace std;
#include <set>

void printset(set<int>&s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{ 
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(5);

	//大小
	cout << s1.size() << endl;

	//判空
	if (!s1.empty())
	{
		cout << "非空" << endl;
	}
	set<int>s2;
	s2.insert(1);
	s2.insert(2);

	//交换
	s1.swap(s2);
	printset(s2);
	printset(s1);
}

int main()
{
	test01();
}

3) 插入删除

#include <iostream>
using namespace std;
#include <set>

void printset(set<int>&s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{ 
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int> s1;

	//插入
	s1.insert(10);
	s1.insert(5);
	s1.insert(20);
	printset(s1);
	//清空
	s1.clear();
	printset(s1);
	//删除1
	s1.insert(10);
	s1.insert(5);
	s1.insert(20);
	set<int>::iterator it = s1.begin();
	s1.erase(it);
	printset(s1);
	//删除2
	s1.erase(s1.begin(), s1.end());
	printset(s1);
	//删除3
	s1.insert(10);
	s1.insert(5);
	s1.insert(20);
	s1.insert(10);
	s1.erase(10);
	printset(s1);
}
int main()
{
	test01();
}

4)查找统计

#include <iostream>
using namespace std;
#include <set>

void printset(set<int>&s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{ 
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	//find(key) 查找key是否存在,存在返回迭代器,不存在,返回set.end();
	//count(key) 统计个数
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	set<int>::iterator it=s1.find(20);
	cout << s1.count(10) << endl;
}
int main()
{
	test01();
}

5) set与multiset

#include <iostream>
using namespace std;
#include <set>

void printset(set<int>&s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{ 
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s;

	pair<set<int>::iterator ,bool> ret=s.insert(10);//返回对组

	if (ret.second)
	{
		cout << "成功" << endl;
	}
	else
	{
		cout << "失败" << endl;
	}

	
	ret = s.insert(10);
	if (ret.second)
	{
		cout << "成功" << endl;
	}
	else
	{
		cout << "失败" << endl;
	}

	multiset<int>ms;
	ms.insert(10);//返回迭代器
	ms.insert(10);

	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
	{
		cout << *it;
	}

}
int main()
{
	test01();
}

6)pair对组的创建

 

#include <iostream>
using namespace std;

void test01()
{
	//第一种方法
	pair<string, int>p("TOM", 20);
	cout << p.first <<"  "<< p.second << endl;

	//第二种方方法
	pair<string, int>p2 = make_pair("jerry", 20);
	cout << p.first << "  " << p.second << endl;
}
int main()
{
	test01();
}

7)自定义数据类型排序

#include <iostream>
using namespace std;
#include <set>

class  Person
{
public:
	Person(string name, int age)
	{
		this->age = age;
		this->name = name;
	}
	string name;
	int age;
};

class comparePerson
{
public:
	bool operator()(const Person& p1, const Person& p2) const
	{
		return p1.age > p2.age;
	}
};
void test01()
{
	//自定义数据类型,都会指定排序规则
	set<Person ,comparePerson>s;
	Person p1("刘备", 20);
	Person p2("关羽", 18);
	Person p3("赵云", 30);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);

	for (set<Person,comparePerson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << it->name << " " << it->age;
	}

}
int main()
{
	test01();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值