如何比较日期大小?

laozi888 2009-11-06 03:35:55
有一个表示时间的结构体
typedef struct  
{
int year;
int mouth;
int day;
int hour;
int minute;
int second;
} Time, *pTime;

如何比较两个的大小啊,要求算法简洁效率高~谢谢大家啊
...全文
292 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cphj 2009-11-10
  • 打赏
  • 举报
回复
如果你要求严格弱序,那么这里确实是大于等于

如果并不要求严格弱序,那么这里就可以看作是大于了

很多算法(如:非稳定排序)数学上并不需要严格弱序

你可以看一下Generic Programming and the STL
laozi888 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cphj 的回复:]
他是对的
函数参数改成引用速度快点
bool operator < (const Time & t1, const Time & t2)
函数体里面代码不变

大于可以借助小于来实现
C/C++ codebooloperator> (const Time& t1,const Time& t2)
{return!(t1< t2);
}


[/Quote]
return!(t1< t2)返回的是大于等于吧???
cphj 2009-11-07
  • 打赏
  • 举报
回复
他是对的
函数参数改成引用速度快点
bool operator < (const Time & t1, const Time & t2)
函数体里面代码不变

大于可以借助小于来实现
bool operator > (const Time & t1, const Time & t2)
{
return !(t1 < t2);
}


laozi888 2009-11-06
  • 打赏
  • 举报
回复
大家帮帮我啊。。。
laozi888 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beyond071 的回复:]
C/C++ codebooloperator< (Time t1, Time t2)
{return t1.year< t2.year&& t1.mouth< t2.mouth&& t1.day< t2.day&& t1.hour< t2.hour&& t1.minute< t2.minute&& t1.second< t2.second;
}
硬算...
[/Quote]
你这对么?。。。。。。。。。。
yyr2008 2009-11-06
  • 打赏
  • 举报
回复
字典式比较
laozi888 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beyond071 的回复:]
C/C++ codebooloperator< (Time t1, Time t2)
{return t1.year< t2.year&& t1.mouth< t2.mouth&& t1.day< t2.day&& t1.hour< t2.hour&& t1.minute< t2.minute&& t1.second< t2.second;
}
硬算...
[/Quote]

那大于怎么写啊?谢谢
danxuezx 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 laozi888 的回复:]
有一个表示时间的结构体C/C++ codetypedefstruct
{int year;int mouth;int day;int hour;int minute;int second;
} Time,*pTime;
如何比较两个的大小啊,要求算法简洁效率高~谢谢大家啊
[/Quote]
就这么个事情怎么看效率呢?
堕落天使 2009-11-06
  • 打赏
  • 举报
回复
同意楼上的。
beyond071 2009-11-06
  • 打赏
  • 举报
回复
bool operator < (Time t1, Time t2)
{
return t1.year < t2.year && t1.mouth < t2.mouth && t1.day < t2.day && t1.hour < t2.hour && t1.minute < t2.minute && t1.second < t2.second;
}

硬算...
ArmStronger 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jackyjkchen 的回复:]
既然你结构体都出来了,解析时间格式都省了;

直接对应量来比,先比年,相同则比更小的单位
[/Quote]
也只能这样啦,若果转换成整数或者字符串来比较的话,转换也挺浪费时间
jackyjkchen 2009-11-06
  • 打赏
  • 举报
回复
既然你结构体都出来了,解析时间格式都省了;

直接对应量来比,先比年,相同则比更小的单位

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧