打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【Boost】month_iterator的妙用
如果使用months,当日期是28号-31号时增减月份,可能会导致问题。
可以使用month_iterator可以解决这个问题。详见下面的例子。

  1. void test_month_iterator()  
  2. {  
  3.     using namespace boost::gregorian;  
  4.   
  5.     date d(2010, 11, 29);  
  6.     d += months(1);  
  7.     assert(d == date(2010, 12, 29));  
  8.     d += months(1);  
  9.     assert(d == date(2011, 1, 29));  
  10.     d += months(1);  
  11.     assert(d == date(2011, 2, 28));  
  12.     // 下面的两步操作不是想要的结果  
  13.     d += months(1);  
  14.     assert(d == date(2011, 3, 31));  
  15.     d -= months(4);  
  16.     assert(d == date(2010, 11, 30));  
  17.   
  18.     // 要解决上面的问题,可以用month_iterator  
  19.     month_iterator itr(date(2010, 11, 29));  
  20.     ++itr;  
  21.     assert(*itr == date(2010, 12, 29));  
  22.     ++itr;  
  23.     assert(*itr == date(2011, 1, 29));  
  24.     ++itr;  
  25.     assert(*itr == date(2011, 2, 28));  
  26.     ++itr;  
  27.     assert(*itr == date(2011, 3, 29));  
  28.     --itr;  
  29.     --itr;  
  30.     --itr;  
  31.     --itr;  
  32.     assert(*itr == date(2010, 11, 29)); // 这样子结果不会发生变化.  
  33. }  



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ArrayList问题
boost库区间range基本原理及使用实例
盘点一道Pandas时间处理实战题目
怎样计算某列日期当月最后一天
这6个日期入门函数,你必须学会,轻松搞定80%的日期问题
日历控件刷新问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服