C++ 自定义数据类型排序(伪函数应用)

1. set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列,元素的插入过程是按顺序规则插入,所以不能指定插入位置

2. set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树,在插入操作和删除操作上比vector快

3. set不可以直接存取元素

4. muitiset 与set的区别:set支持唯一键值,每个元素值只出现一次,而muitiset中同一值可以出现多次.

5. 不可以直接修改set或muitiset 容器中的元素值,因为该类容器是自动排序的,如果可以修改一个元素值,必须先删除原有的元素,在插入新的元素

元素集合排序

set less 是升序排序

set greater 是降序排序

代码示例(对于一些新的用法会以注释的方式标准出来,很重要!!!!)

#include
using namespace std;
#include
#include"list"
#include"set"
//是一个集合,元素唯一,自动排序 不能按照数组的方式插入元素
void main11()
{

	set setl;
	for (int i = 0; i < 5; i++)
	{
		int tmp = rand();
		setl.insert(tmp);
	}
	//插入元素,重复队列
	setl.insert(100);
	setl.insert(100);
	setl.insert(100);

	for (set::iterator it = setl.begin(); it != setl.end(); it++)
	{
		cout << *it << " ";
	}
	//删除集合
	while (!setl.empty())
	{
		set::iterator it = setl.begin();
		cout << *it << endl;
		setl.erase(setl.begin());
	}
}

void main33()
{
	set setl;
	set>set2;
	set>set3;//从大到小

	for (int i = 0; i < 5; i++)
	{
		int tmp = rand();
		set3.insert(tmp);
	}
	//从小到大
	for (set>::iterator it = set3.begin(); it != set3.end(); it++)
	{
		cout << *it << endl;
	}
	//插入元素,重复队列
	setl.insert(100);
	setl.insert(100);
	setl.insert(100);
}
class Student
{
public:
	Student(char*name, int age)
	{

	}
public:
	char name[64];
	int age;
};
//伪函数 算符 是重载了"()"操作符的普通类对象,从语法上讲,它与普通函数行为类似
struct FuncStudent
{
	bool operator()(const Student*left, const Student &right)
	{
		if (left.age < right.age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

};
void main33()
{

	Student s1("s1", 31);
	Student s2("s2", 41);
	Student s3("s3", 51);
	Student s4("s4", 61);
	Student s5("s4", 61);//如果有2个61就不会插入成功,因为是唯一唯一唯一!!!!!!!!!

	set set1;
	set1.insert(s1);
	set1.insert(s2);
	set1.insert(s3);
	set1.insert(s4);
	set1.insert(s5);///err
	//遍历

	for (set>::iterator it = set3.begin(); it != set3.end(); it++)
	{
		cout << it->age<<"	"<name<< endl;
	}
}
void main44()
{
	Student s1("s1", 31);
	Student s2("s2", 41);
	Student s3("s3", 51);
	Student s4("s4", 61);
	Student s5("s4", 61);
	set set1;
	pair::iterator, bool>pair1 = set1.insert(s1);
	////pair对组,可以将二个值视为一个单元,pair存放的二个值可以一样也可以不一样.可以是自定义.
	//pair,first是第一个值是t1类型
	//pair,second是第一个值是t2类型
	if (pairl.second == true)

	{
		cout << "插入成功" << endl;

	}
	else
	{
		cout << "插入失败" << endl;
	}
	set1.insert(s2);
	pair::iterator, bool>pair1 = set1.insert(s5);

	for (set>::iterator it = set3.begin(); it != set3.end(); it++)
	{
		cout << it->age << "	" << it->name << endl;
	}
}
//对于复杂的数据类型 (比如类)
void main55()
{
	set setl;
	for (int i = 0; i < 5; i++)
	{
		set1.insert(i + 1);
	}
	for (set>::iterator it = set3.begin(); it != set3.end(); it++)
	{
		cout << *it << endl;
	}
	cout << endl;
	set::iterator it0 = setl.find(5);
	int num1 = set1.count(5);
	cout << "num1:" << num1 << endl;
	set::iterator it1 = set1.lower_bound(5);//大于等于5的元素 的迭代器的位置
	cout << "it1:" << *it1 << endl;
	set::iterator it1=set1.lower_bound(5);//大于5的元素 的迭代器的位置
	cout << "it2:" << *it2 << endl;
//
  setl.equal_range(5);
}
void main()
{
	main11();

	main33();
	main44();
 	main55();
	cout << "hello..." << endl;
	system("pause");
	return;
}

予人玫瑰,手有余香,小羊伴你一路同行~~~~

展开阅读全文

页面更新:2024-03-06

标签:小羊   升序   变体   数据结构   数组   示例   注释   容器   数据类型   函数   顺序   单元   元素   位置   类型   操作   方式   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top