c++set基本操作,自定义类型排序,查找和对组

about Pair 对组

template<class T1,class T2> struct pair

//T1是第一个值的数据类型,T2是第二个值的数据类型

//功能:pair是一个模板类型,将一对值组合,这一对值可以具有不同的数据类型T1&&T2

1. pair<int,string> a;//表示a中有两个类型一个int型,一个string型,如果创建时候没有对其初始化就调用默认构造函数对其初始化。

2. _pair<string,string> a(“Rita”,”Hermione”);

_pair 对象的操作:

_pair类只有两个元素(first/second),可以直接使用点操作符访问(string name=pair.second)。


// 第四次练习4.25start.cpp : 定义控制台应用程序的入口点。
#define _CRT_SECURE_NO_WARNINGS
#include<functional>//不加会出现error:"greater"未声明的标识符
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
//1	set集合有序(默认从小到大)唯一(红黑二叉树这种数据结构的变体)
void play06()
{
	set<int> set1;					//定义一个set容器
	for (int i = 0; i < 5; ++i)		//向容器中插入五个随机数
	{
		int tmp = rand();
		set1.insert(tmp);
	}
	set1.insert(100);				//向容器中插入三个相同的数
	set1.insert(100);
	set1.insert(100);
	//打印输出
	for (set<int>::iterator it = set1.begin(); it != set1.end(); it++)//用迭代器遍历整个容器
	{
		cout << *it << " ";
	}
	cout << endl;
	//删除集合
	cout << "删除集合:";
	while (!set1.empty())
	{
		set<int>::iterator it = set1.begin();
		cout << *it << " ";
		set1.erase(set1.begin());//每次删除集合的首元素
	}
	cout << endl;
}
void play07()
{
	//2 集合  默认排序规则的是从小到大,也可以从大到小排序
	set<int> set1;					//定义一个set容器
	for (int i = 0; i < 5; ++i)		//向容器中插入五个随机数
	{
		int tmp = rand();
		set1.insert(tmp);
	}
	set1.insert(100);				//向容器中插入三个相同的数
	set1.insert(100);
	set1.insert(100);
	//打印输出
	for (set<int>::iterator it = set1.begin(); it != set1.end(); it++)//用迭代器遍历整个容器
	{
		cout << *it << " ";
	}
	cout << endl;
	//删除集合
	cout << "删除集合:";
	while (!set1.empty())			//从小到大删除元素
	{
		set<int,greater<int>>::iterator it = set1.begin();
		cout << *it << " ";
		set1.erase(set1.begin());							
	}
	cout << endl;
}
//3.自定义数据类型排序
//仿函数 函数对象  重载()操作 进行比较大小
//学生包含姓名和年龄属性,要求任意插入几个学生对象到set容器中,是的学生按年龄升序排列
class Student
{
public:
	Student(char *name, int age)
	{
		strcpy(this->name, name);
		this->age=age;
	}
public:
	char name[32];
	int age;
};
//函数对象
struct StuFunctor
{
	bool operator()	(const Student &left, const Student &right)
	{
		return(left.age < right.age);//按照年龄大小排序,年龄小的在前面
	}
};
void play08()
{
	set<Student, StuFunctor> set1;//定义一个装有Student自定义类型的容器,按照学生age大小从小到大自动排序
	Student s1("Rita",21);
	set1.insert(s1);
	set1.insert(Student("Hermione", 22));
	set1.insert(Student("Mike", 23));
	set1.insert(Student("Ding", 24));
	//打印输出
	//注意:定义迭代器的时候要加上StuFountor这个
	for (set<Student, StuFunctor>::iterator it = set1.begin(); it != set1.end(); it++)
	{
		cout << (*it).age << " ";
	}
	cout << endl;
}
//4	set集合查找功能
void play09()
{
	int i = 0;
	set<int> set1;
	for (i = 1; i < 10; i++)
	{
		set1.insert(i);
	}
	//打印输出
	for (set<int>::iterator it = set1.begin(); it != set1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	set<int>::iterator it1 = set1.lower_bound(5);	//大于等于5迭代器(在值为5的位置,所以*it1输出5)
	set<int>::iterator	it2 = set1.upper_bound(5);	//大于5迭代器(在值为6的位置,所以*it1输出6)
	//通过迭代器进行元素的操作
	cout << "it1:" << *it1 << " " << "it2:" << *it2 << endl;
	pair<set<int>::iterator, set<int>::iterator> pairIt = set1.equal_range(5);
	set<int>::iterator it3 = pairIt.first;//获取第一个
	set<int>::iterator it4 = pairIt.second;//获取第二个
	cout << "it3:" << *it3 << " " << "it4:"<< *it4 << endl;
}
int main()
{
	play06();
	play07();
	play08();
	play09();

	system("pause");
	return 0;
}

运行结果:











发布了66 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/RitaAndWakaka/article/details/80218422