3.8.5 set查找和统计

本文介绍了如何在C++中使用set容器进行数据插入、查找元素和统计元素出现次数的示例。通过`test01`和`test02`函数展示了set的查找操作及`count`方法的应用。

 

#include<iostream>
#include<set>
using namespace std;
//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;
	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	set<int>::iterator pos = s1.find(30);
	if (pos != s1.end())
	{
		cout << "找到元素:" << *pos << endl;
	}
	else
	{
		cout << "找不到元素:" << endl;
	}
}

void test02()
{
	set<int>s1;
	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(30);
	s1.insert(30);
	s1.insert(50);
	s1.insert(40);
	
	//统计30 的个数
	int num = s1.count(30);
	//对于set而言 ,统计只有1 或者 0
	cout << "num = " << num << endl;

}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值