STL-2.19

本文介绍了C++中的栈(stack)数据结构,包括其基本概念、常用函数以及代码示例。同时详细讲解了set、multiset和unordered_set集合,展示了它们的特性、函数和使用方法,特别是排序和查找操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

stack

stack的定义和结构

stack是一种先进先出的数据结构

stack的常用函数

push(x):在栈顶插入元素

pop():弹出栈顶元素

top():返回栈顶元素

empty():检查栈是否为空

size():返回栈中的元素个数

tips:将一个数组的元素依次放入栈中,再依次取出,则可以将数组翻转。

stack不能遍历。

代码示例:

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

int main()
{
	stack<int> myStack;

	//向栈中插入元素
	myStack.push(10);
	myStack.push(20);
	myStack.push(30);
	myStack.push(40);

	//获取栈顶元素
	cout << "栈顶元素" << myStack.top() << endl;

	//弹出栈顶元素
	myStack.pop();

	//再次获得栈顶元素
	cout << "弹出一个元素后的栈顶元素" << myStack.top() << endl;

	//检查栈是否为空
	if (myStack.empty())
		cout << "栈为空" << endl;
	else
		cout << "栈不为空" << endl;

	//获取栈的大小
	cout << "获取栈的大小" << myStack.size() << endl;

	return 0;
}

set

set集合

set是一种容器,用于存储一组唯一元素,并按照一定是排序规则进行排序。set中的元素默认按照升序排序。

set中的元素是唯一的,即不允许重复元素存在。当插入一个重复元素时,set会自动忽略该元素。

函数:

insert ():插入

erase():移除

find():查找集合中的元素

lower_bound():返回第一个不小于给定值的迭代器

upper_bound():返回第一个大于给定值的迭代器

size() ,empty(),clear(),begin(),end(),与之前一样

rbegin():返回指向集合末尾位置的逆向迭代器

rend():返回指向集合起始位置的逆向迭代器

修改set比较函数是常见方法,multiset类似

1)第一种


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

int main()
{
	//将升序改为降序
	set <int, greater<int>> mySet;

	//向集合插入元素
	mySet.insert(25);
	mySet.insert(25);
	mySet.insert(25);
	mySet.insert(25);

	//枚举遍历输出
	for (const auto& num : mySet)
	{
		cout << num << " ";
	}
	cout << endl;

	return 0;
}

2)第二种

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

struct MyCompare
{
	bool operator ()(const int& a, const int& b) const
	{
		//自定义比较逻辑
		return a > b;//改为逆序
	}
};

int main()
{
	set <int, MyCompare> mySet;
	//向集合插入元素
	mySet.insert(25);
	mySet.insert(43);
	mySet.insert(42);
	mySet.insert(40);

	//枚举遍历输出
	for (const auto& num : mySet)
	{
		cout << num << " ";
	}
	cout << endl;
	return 0;
}

代码示例

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

int main()
{

	set<int > mySet;
	       // 向集合插入元素
			mySet.insert(5);
			mySet.insert(2);
			mySet.insert(8);
			mySet.insert(2);
		
			//枚举遍历输出
			cout << "Set element:";
			for (const auto& num : mySet)
			{
				cout << num << " ";
			}
			cout << endl;

			//查找元素
			int searchValue = 5;
			auto it = mySet.find(searchValue);//返回iter
			if (it != mySet.end())
				cout << searchValue << " found in the set" << endl;
			else 
				cout << searchValue << "not  found in the set" << endl;

			//移除元素
			int removeValue = 2;
			mySet.erase(removeValue);

			//再次遍历
			cout << " set在移除元素后";
			for (const auto& elem : mySet)
			{
				cout << elem << " ";
			}
			cout << endl;

			// 清空集合
			mySet.clear();

			if (mySet.empty())
				cout << " set is empty" << endl;
			else
				cout << " set is not empty" << endl;


	return 0;
}

multiset 多重集合

multiset是一种容器,用于存储一组唯一元素,并按照一定是排序规则进行排序。

不同之处是,multiset容器允许存储重复元素,元素可以重复出现,

函数与set相同。

重点是find(),

erase():这个直接删除一个元素,它会全部删,若是删除一个元素,先用find()找到迭代器,再删。

示例:


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

int main()
{

	multiset<int > mySet;
	       // 向集合插入元素   2258 允许存在重复元素
			mySet.insert(5);
			mySet.insert(2);
			mySet.insert(8);
			mySet.insert(2);
		
			//枚举遍历输出
			cout << "multiSet element:";
			for (const auto& num : mySet)
			{
				cout << num << " ";
			}
			cout << endl;

			//查找元素
			int searchValue = 5;
			auto range = mySet.equal_range(searchValue);//返回iter
			if (range.first != range.second)
				cout << searchValue << " found in the multiset" << endl;
			else 
				cout << searchValue << "not  found in the multiset" << endl;

			//移除元素
			int removeValue = 2;
			mySet.erase(removeValue);

			//再次遍历
			cout << " multiset在移除元素后";
			for (const auto& elem : mySet)
			{
				cout << elem << " ";
			}
			cout << endl;

			// 清空多重集合
			mySet.clear();

			if (mySet.empty())
				cout << " multiset is empty" << endl;
			else
				cout << " multiset is not empty" << endl;


	return 0;
}

  

unordered_set无序集合(仅作了解)

unordered_set是一种容器,用于存储一组唯一元素,并且没有特定的顺序。它使用哈希表来实现存储和访问,因此插入、删除和查找的时间复杂度为常数。

元素是唯一的,不允许重复元素存在。当插入一个重复元素,会自动忽略。

函数:

insert ():插入

erase():移除

find():查找无序集合中的元素

count ():返回元素在集合中出现的次数

size(): 返回元素数量

示例:

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

int main()
{

	unordered_set<int > mySet;
	       // 向集合插入元素   528 ,无序,唯一元素
			mySet.insert(5);
			mySet.insert(2);
			mySet.insert(8);
			mySet.insert(2);
		
			//枚举遍历输出
			cout << "unordered_set element:";
			for (const auto& num : mySet)
			{
				cout << num << " ";
			}
			cout << endl;

			//查找元素
			int searchValue = 5;
			auto it = mySet.find(searchValue);//返回iter
			if (it !=mySet.end())
				cout << searchValue << " found in the unordered_set" << endl;
			else 
				cout << searchValue << "not  found in the unordered_set" << endl;

			//移除元素
			int removeValue = 2;
			mySet.erase(removeValue);

			//再次遍历
			cout << " unordered_set在移除元素后";
			for (const auto& elem : mySet)
			{
				cout << elem << " ";
			}
			cout << endl;

			// 清空多重集合
			mySet.clear();

			if (mySet.empty())
				cout << " unordered_set is empty" << endl;
			else
				cout << " unordered_set is not empty" << endl;


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值