set

本文深入探讨了C++标准模板库(STL)中set与multiset容器的使用方法,包括插入、查找、删除操作及排序机制。通过实例演示了如何处理自定义类型数据,并介绍了pair类型的运用。

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

set
在这里插入图片描述
multiset
在这里插入图片描述
序列式怎么插入的怎么输出,关联式会排序,从小到大

void test1()
{
	set<int> s1;
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	printset(s1);// 2 3 4 5

	if (s1.empty())
	{
		cout << "kong" << endl;
	}
	else {
		cout << "bukong" << endl;
	}
	s1.erase(s1.begin());//删第一个 3 4 5
	s1.erase(3);//删除元素3 得到 4 5
	printset(s1);
}

在这里插入图片描述

//查找
void test2()
{
	set<int> s1;
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	printset(s1);// 2 3 4 5

	//对于set 没有value  key就是value
	set<int>::iterator  pos = s1.find(3);
	//判断是否找到
	if (pos != s1.end())
	{
		cout << "找到了,值为" << *pos << endl;
	}
	else {
		cout << "没找到" << endl;
	}
	//count(key)查找key元素的个数,对于set而言 结果0或者1
	int num=s1.count(3);

	//lower_bound(keyElem)  //返回第一个key>=keyElem元素的迭代器
	set<int>::iterator it= s1.lower_bound(3);
	if (it != s1.end())
	{
		cout <<"找到了"<< *it << endl;

	 }
	else {
		cout << "无" << endl;
	}
	//upper_bound(keyElem)  返回第一个key>keyElem元素的迭代器
	set<int>::iterator it1 = s1.upper_bound(3);
	if (it1 != s1.end())
	{
		cout << "找到了" << *it1 << endl;

	}
	else {
		cout << "无" << endl;
	}
	//equal_range(keyElem)返回容器中key与keyElem相等的上下限的两个迭代器
	//上下限 就是upper_bound lower_bound
	s1.equal_range(4);
	pair<set<int>::iterator, set<int>::iterator> ret= s1.equal_range(4);
	//ret.first;//第一个迭代器
	if (ret.first != s1.end())
	{
		cout << "下限是" << *(ret.first)<< endl;
	}
	else {
		cout << "没找到" << endl;
	}
	if (ret.second != s1.end())
	{
		cout << "上限是" << *(ret.second) << endl;
	}
	else {
		cout << "没找到" << endl;
	}

}

pair

// pair.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
void test01()
{
	//第一种创建方式
	pair<string, int> p(string("aaa"), 100);
	cout << "姓名" << p.first << endl;
	cout << "年龄" << p.second << endl;
	//第二种创建方式
	pair<string, int> p2 = make_pair("bbb", 200);
	cout << "姓名" << p2.first << endl;
	cout << "年龄" << p2.second << endl;



}

int main()
{
	test01();
    return 0;
}


set不允许插入重复的值

//set不允许插入重复的值
void test3()
{
	set<int> s1;
	pair<set<int>::iterator,bool> ret= s1.insert(10);
	if (ret.second)
	{
		cout<<"插入成功 "<< endl;
	}
	else {
		cout << "插入失败" << endl;
	}
	ret = s1.insert(10);
	if (ret.second)
	{
		cout << "插入成功 " << endl;
	}
	else {
		cout << "插入失败" << endl;
	}
	s1.insert(10);
	printset(s1);//10 只有一个
	//multiset 允许插入重复值
	mul.insert(10);
	mul.insert(10);//两个都插入成功
}

排序

void test04()
{
	//set<int> s1;
	set<int, myCompare>s1;//因为需要放入类型 ,所以是用仿函数
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	//printset(s1);// 2 3 4 5
	//想要从大到小排序
	//在插入之前制定排序规则
	for (set<int, myCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

set容器插入自定义类型数据

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

	}
public:
	string name;
	int age;
};
//仿函数
class comPerson
{
public:
	bool operator()(const Person &p1, const Person & p2)
	{
		return p1.age > p2.age;
	}
};
void test5()
{
	set<Person, comPerson> s1;
	Person p1("aaa", 1);
	Person p2("bbb", 2);
	Person p3("ccc", 3);
	
	//s1.insert(p1);
	//s1.insert(p2);
	//s1.insert(p3);
	//插入失败 因为插入时 set容器不知道怎么对它排序
	//插入自定义类型,要先制定好排序规则
	//排序规则写在	set<Person> s1 内

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	for (set<Person, comPerson>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "姓名" << (*it).name << "   年龄" << (*it).age << endl;
	}
}
在 Java 中,遍历 Set 集合可以通过多种方法实现,这些方法包括使用迭代器、增强的 for 循环以及 Stream API。以下是详细的介绍和示例: ### 使用迭代器遍历 迭代器是一种传统的遍历方式,它允许在遍历过程中执行删除操作[^1]。 ```java Set<String> set = new HashSet<>(); // 假设已经向set中添加了一些元素 Iterator<String> it = set.iterator(); while (it.hasNext()) { String str = it.next(); System.out.println(str); } ``` 如果需要在遍历过程中删除特定的元素,可以这样做[^2]: ```java Set<Integer> numberSet = new HashSet<>(); // 添加一些元素到集合 numberSet.add(1); numberSet.add(2); numberSet.add(3); numberSet.add(4); numberSet.add(5); Iterator<Integer> iterator = numberSet.iterator(); while (iterator.hasNext()) { Integer num = iterator.next(); // 删除偶数 if (num % 2 == 0) { System.out.println("Removing: " + num); iterator.remove(); // 在迭代过程中删除元素 } } ``` ### 使用增强的 for 循环 增强的 for 循环提供了一种简洁的方式来遍历 Set 集合,但它不支持在遍历过程中修改集合[^2]。 ```java Set<String> set = new HashSet<>(); // 假设已经向set中添加了一些元素 for (String str : set) { System.out.println(str); } ``` ### 使用 Stream API Java 8 引入了 Stream API,这为处理集合提供了一种非常强大的功能,包括遍历集合[^2]。 ```java Set<String> set = new HashSet<>(); // 假设已经向set中添加了一些元素 set.stream().forEach(System.out::println); ``` 以上就是几种常用的遍历 Set 集合的方法。每种方法都有其适用场景,选择合适的方法取决于具体的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值