vector容器与set容器结构体排序

用日期为例vector容器中结构体排序与set容器中结构体排序以及处理日期问题时需要注意的事项

1.不含结构体vector容器排序

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	vector<int>a;
	a.push_back(5);
	a.push_back(1);
	a.push_back(2);
	a.push_back(9);
	for (vector<int>::iterator it = a.begin();it != a.end();it++)
	{
		cout << *it << " ";
	}
}

此时输出顺序为加入vector容器中的顺序即5 1 2 9
对数组进行sort后

	sort(a.begin(), a.end());
	for (vector<int>::iterator it = a.begin();it != a.end();it++)
	{
		cout << *it << " ";
	}

输出为1 2 5 9
在这里插入图片描述

2.含有结构体vector容器排序

本处举例为将日期按照从小到大顺序排序

struct Date {//构建结构体并且重载<运算符
    int year; 
    int month; 
    int day;
};
bool cmp(Date a,Date b)
{
	if (a.year != b.year)//如果年份不同那么年份较小的在前面
		return a.year < b.year;
	if (a.month != b.month)//只有年份相同才能进行到这一步,如果年份不同肯定可以比出大小并返回结束,年份相同时月份小的在前
		return a.month < b.month;
	//如果年份月份都相同才能进入到这一步,返回日期小的在前
	return a.day < b.day;
}
int main(){
	vector<Date>myDate;
	myDate.push_back({ 2001,8,24 });
	myDate.push_back({ 2001,6,19 });
	myDate.push_back({ 1977,1,26 });
	myDate.push_back({ 1988,12,28 });
	
	for (int i = 0; i < myDate.size(); i++)//输出排序前日期
	{
		cout << myDate[i].year << "年" << myDate[i].month << "月" << myDate[i].day << "日"<<endl;
	}
	cout << endl;
	sort(myDate.begin(),myDate.end(),cmp);//排序
	for (int i = 0; i < myDate.size(); i++)//输出排序后日期
	{
		cout << myDate[i].year << "年" << myDate[i].month << "月" << myDate[i].day << "日" << endl;
	}
}

在这里插入图片描述

3.set容器中结构体排序

利用set可以存储排好序的并且数据不重复的数组,在存储日期时,利用set特性可以避免日期重复并且利用重载<运算符可以自动使日期实现从小到大排序。

struct Date {//构建结构体并且重载<运算符
	int year, month, day;
	bool operator<(const Date& mydate)const {
		if (year !=  mydate.year)//先比较年返回较小的年份
			return year <  mydate.year;
		if (month !=  mydate.month)//如果年相同返回较小的月
			return month <  mydate.month;

		return day <  mydate.day;//如果年和月都相同才能到最后返回较小的日
	}
};
bool cmp(Date a,Date b)
{
	if (a.year != b.year)
		return a.year < b.year;
	if (a.month != b.month)
		return a.month < b.month;
	return a.day < b.day;
}
int main()
{
	vector<Date>myDate;
	myDate.push_back({ 2001,8,24 });
	myDate.push_back({ 2001,8,24 });
	myDate.push_back({ 2001,6,19 });
	myDate.push_back({ 1977,1,26 });
	myDate.push_back({ 1988,12,28 });
	cout << "vector存储排序前:" << endl;
	for (int i = 0; i < myDate.size(); i++)
	{
		cout << myDate[i].year << "年" << myDate[i].month << "月" << myDate[i].day << "日"<<endl;
	}
	cout << endl;
	sort(myDate.begin(),myDate.end(),cmp);
	cout << "vector存储排序后:" << endl;
	for (int i = 0; i < myDate.size(); i++)
	{
		cout << myDate[i].year << "年" << myDate[i].month << "月" << myDate[i].day << "日" << endl;
	}
	cout << endl;
	cout << "set存储自动排序后:" << endl;
	set<Date>mySetDate;
	mySetDate.insert({ 2001,8,24 });
	mySetDate.insert({ 2001,6,19 });
	mySetDate.insert({ 1977,1,26 });
	mySetDate.insert({ 1988,12,28 });
	mySetDate.insert({ 2001,8,24 });
	for (set<Date>::iterator it = mySetDate.begin(); it != mySetDate.end(); it++)
	{
		cout << (*it).year << "年" << (*it).month << "月" << (*it).day << "日" << endl;
	}
}

在这里插入图片描述

4.vector容器set容器总结及注意事项

1.在遍历vector容器时可以利用vector[i]进行遍历,也可以利用指针进行遍历,但是在遍历set容器时只能用指针进行遍历
2.在vector容器中想要实现结构体排序利用的是加一个函数作为参数,在函数中编写要求即可,在set容器中想要实现结构体排序则是重载<运算符进行实现。
3.vector容器中存储的数据可以重复,但**set容器中的数据不允许重复,只会保留一个,**在处理日期等可能会重复但是不想要重复的日期的时候用set很好用。

处理日期问题时的tips

1.如果涉及到日期的反复判断是否符合要求,将月份存入一个数组中比较方便

int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

直接使用days[几月]即可表示这个月有多少天。

2.在遇到二月时要特殊处理判断是否为闰年,闰年判断条件为:年份是否能整除400或者可以整除4并且不能整除100,两者满足其一这一年即为闰年。如果判断为润年后则29号也为合法日期。
3.日期可能产生重复,题目中重复的日期是否需要筛出,根据题意注意一下判断即可。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值