【发布时间】:2013-04-18 10:39:59
【问题描述】:
我正在尝试用一种惯用的方式来写这篇文章
boost::gregorian::month_iterator start(boost::gregorian::date(1901,1,1),1);
boost::gregorian::date end(2000,12,31);
int i = 0;
while(start <= end) {
boost::gregorian::gregorian_calendar::day_of_week_type x = (*start).day_of_week();
if(x==0)
++i;
}
这很好,但我想知道是否有这样的写法
boost::gregorian::month_iterator start(boost::gregorian::date(1901,1,1),1);
boost::gregorian::month_iterator end(boost::gregorian::date(2000,12,31));
const int i = std::count_if(start,end,[](boost::gregorian::month_iterator& start) {
boost::gregorian::gregorian_calendar::day_of_week_type x = (*start).day_of_week();
return (x==0);
});
相同的结果,但我认为它是更漂亮的代码,它更像是一种函数式方法(我喜欢),而且我想要实现的意图更清晰。 我有一种感觉我必须使用 any_range 但我不确定在这种情况下如何,因为 month_iterator 似乎没有前向迭代器的所有正常语义
【问题讨论】:
标签: c++ date boost c++11 iterator