打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
【Boost】month_iterator的妙用
wtkc
>《date_time》
2014.10.26
关注
如果使用months,当日期是28号-31号时增减月份,可能会导致问题。
可以使用month_iterator可以解决这个问题。详见下面的例子。
[cpp]
view plain
copy
print
?
void test_month_iterator()
{
using namespace boost::gregorian;
date d(2010, 11, 29);
d += months(1);
assert(d == date(2010, 12, 29));
d += months(1);
assert(d == date(2011, 1, 29));
d += months(1);
assert(d == date(2011, 2, 28));
// 下面的两步操作不是想要的结果
d += months(1);
assert(d == date(2011, 3, 31));
d -= months(4);
assert(d == date(2010, 11, 30));
// 要解决上面的问题,可以用month_iterator
month_iterator itr(date(2010, 11, 29));
++itr;
assert(*itr == date(2010, 12, 29));
++itr;
assert(*itr == date(2011, 1, 29));
++itr;
assert(*itr == date(2011, 2, 28));
++itr;
assert(*itr == date(2011, 3, 29));
--itr;
--itr;
--itr;
--itr;
assert(*itr == date(2010, 11, 29)); // 这样子结果不会发生变化.
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
ArrayList问题
boost库区间range基本原理及使用实例
盘点一道Pandas时间处理实战题目
怎样计算某列日期当月最后一天
这6个日期入门函数,你必须学会,轻松搞定80%的日期问题
日历控件刷新问题
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×