set容器的基本操作

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//set mutiset 头文件
#include <set>
//set容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复值 


set容器常用函数
使用时注意包含头文件<set>    std::set and std::multiset associative containers
/* 
s.begin()      返回set容器的第一个元素
 
s.end()      返回set容器的最后一个元素
 
s.clear()       删除set容器中的所有的元素
 
s.empty()     判断set容器是否为空
 
s.insert()      插入一个元素
 
s.erase()       删除一个元素
 
s.size()     返回当前set容器中的元素个数
*/

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;
	//关联式容器,根据K值进行排序  从小到大排序
	s1.insert(5);
	s1.insert(1);
	s1.insert(7);
	s1.insert(2);
	s1.insert(9);
	PrintSet(s1);

	s1.erase(s1.begin());
	s1.erase(2);
	PrintSet(s1);
}



void test02()
{
	set<int>s1;
	s1.insert(5);
	s1.insert(1);
	s1.insert(7);
	s1.insert(2);
	s1.insert(9);


	//1.find(key)  查找key是否存在,存在则返回该键元素的迭代器 没有则返回set.end()
    //对于set容器 没有 value  key就是value 
	set<int>::iterator  pos = s1.find(2);
	//判断是否找到
	if (pos != s1.end())
	{
		cout << "找到了,值为:" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//2.count(key)  查找key的个数
	cout<<"元素为1的个数有"<<s1.count(1)<<"个"<<endl;


	//3.lower_bound(keyElement)  返回第一个key >= keyElement元素的迭代器
	set<int>::iterator pos2 =s1.lower_bound(3);
	if (pos2 != s1.end())
	{
		cout << "找到了,值为:" << *pos2 << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//4.upper_bound(keyElement)  返回第一个key > keyElement元素的迭代器
	set<int>::iterator pos3 = s1.lower_bound(3);
	if (pos3 != s1.end())
	{
		cout << "找到了,值为:" << *pos3 << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//5.equal_range(keyElement)  返回key = keyElement相等的上下限的两个迭代器
	//上下限 就是 lower_bound 和 upper_bound
	pair<set<int>::iterator, set<int>::iterator> ret  = s1.equal_range(2);
	//获取第一个值 ret.first
	if (ret.first != s1.end())
	{
		cout << "找到了equal_range的第一个值lower_bound为:" << *(ret.first) << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
	//获取第二个值 ret.second
	if (ret.second != s1.end())
	{
		cout << "找到了equal_range的第二个值upper_bound为:" << *(ret.second) << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

//set容器,不允许插入重复的
void test03()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(10);
	PrintSet(s1);//插入两个值,只能打印出一个 说明第二次插入已经失败了
	//判断再次插入是否插入成功
	pair<set<int>::iterator, bool> p = s1.insert(10);
	if (p.second)
	{
		cout<<"插入成功"<<endl;
	}
	else
	{
		cout << "插入失败" << endl;
	}
}

//指定排序规则
//仿函数
class myCompare
{
public:
	bool  operator()(int v1, int v2)
	{
		return v1 > v2;
	
	}


};

//set容器排序
void test04()
{
	set<int,myCompare>s1;
	s1.insert(5);
	s1.insert(1);
	s1.insert(7);
	s1.insert(2);
	s1.insert(9);
	//PrintSet(s1);//内部已排序好了
	//从大到小排序  必须在插入前就指定排序规则
	for (set<int, myCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << endl;
	}

}

//set 容器插入自定义数据类型
class Person
{
public:
	Person(string Name, int age)
	{
		this->m_Name = Name;
		this->m_age = age;

	}


	string m_Name;
	int m_age;

};

class MyCompare
{
public:
	bool operator()(const Person &p1, const Person &p2)
	{
	
		if (p1.m_age > p2.m_age)
		{
			return true;

		}
		else
			return false;
	}



};
void test05()
{
	Person p1("大娃",100);
	Person p2("二娃", 80);
	Person p3("三娃", 10);
	Person p4("四娃", 60);
	Person p5("五娃", 200);
	set<Person,MyCompare> s1;
	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);
	s1.insert(p5);
	//会编译失败。set不知道以什么方式排序  利用仿函数 指定排序规则 方法与上相同
	for (set<Person, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << "   年龄:" << (*it).m_age << " " << endl;

	}
}


int main()
{
	//test01();
	//test02();
	//test03();
	//test04();
	test05();
	std::cout << "Hello World!\n"; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++ STL库中的set容器是一个用于存储元素的关联容器,它的元素默认按照升序排列,并且保证元素的唯一性。set容器基本操作包括: 1. 插入元素:使用insert()函数向set容器中插入元素。插入元素的时间复杂度为O(log n)。 ```c++ #include <iostream> #include <set> int main() { std::set<int> s; s.insert(3); s.insert(1); s.insert(4); s.insert(1); // 重复元素不会被插入 s.insert(5); for (auto x : s) { std::cout << x << " "; } std::cout << std::endl; return 0; } ``` 上述代码输出:1 3 4 5 2. 删除元素:使用erase()函数删除set容器中的元素,可以指定要删除的元素值,也可以指定一个迭代器范围。删除元素的时间复杂度为O(log n)。 ```c++ #include <iostream> #include <set> int main() { std::set<int> s{3, 1, 4, 1, 5}; s.erase(1); // 删除元素值为1的元素 for (auto x : s) { std::cout << x << " "; } std::cout << std::endl; return 0; } ``` 上述代码输出:3 4 5 3. 查找元素:使用find()函数和count()函数查找set容器中的元素,find()函数返回一个指向要查找的元素的迭代器,如果元素不存在,则返回end()迭代器。count()函数返回要查找的元素在set容器中出现的次数,因为set容器中的元素保证唯一性,所以count()函数的返回值只能是0或1。查找元素的时间复杂度为O(log n)。 ```c++ #include <iostream> #include <set> int main() { std::set<int> s{3, 1, 4, 1, 5}; auto it = s.find(4); if (it != s.end()) { std::cout << "4 is found" << std::endl; } else { std::cout << "4 is not found" << std::endl; } std::cout << "1 appears " << s.count(1) << " times" << std::endl; std::cout << "2 appears " << s.count(2) << " times" << std::endl; return 0; } ``` 上述代码输出:4 is found,1 appears 1 times,2 appears 0 times。 除了上述基本操作外,set容器还支持迭代器操作和比较操作。更详细的使用方法可以参考C++ STL库的set容器文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael.Scofield

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值