0

I'm trying to come up with an idomatic way of writing this

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;
}

This is fine, but I wondered if there was a way of writing it like

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);
});

Same result, but I think it's prettier code, it's more like a functional approach (which I like) and the intention of what I'm trying to achieve is clearer. I have a feeling I have to use any_range<> but I'm not really sure how in this case as the month_iterator doesn't seem to have all the normal semantics of forward iterators

4

1 回答 1

0

没有简单的方法可以做到这一点。首先,您应该std::iterator_traits像这样专门化

namespace std {

template<>
struct iterator_traits<boost::gregorian::month_iterator>
{
   typedef ptrdiff_t difference_type;
   typedef boost::gregorian::month_iterator value_type;
   typedef boost::gregorian::month_iterator& reference;
   typedef boost::gregorian::month_iterator* pointer;
};

}

其次,您不能使用 any boost::range,因为没有operator ==for 两个迭代器并且没有operator * const. 因此,您应该编写自己的类,可能派生自boost::iterator_adaptor,但是,我认为,这个迭代器根本不是为这种用途而创建的。

我正在写一个小例子,这不是有效的,但它可以工作并且没有过于复杂。

#include <boost/range.hpp>
#include <boost/range/any_range.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/shared_ptr.hpp>

#include <vector>

namespace std {

template<>
struct iterator_traits<boost::gregorian::month_iterator>
{
   typedef ptrdiff_t difference_type;
   typedef boost::gregorian::month_iterator value_type;
   typedef boost::gregorian::month_iterator& reference;
   typedef boost::gregorian::month_iterator* pointer;
};

}

class month_iterator : public boost::iterator_adaptor
<
   month_iterator,
   boost::gregorian::month_iterator*,
   boost::gregorian::month_iterator::value_type,
   boost::bidirectional_traversal_tag,
   boost::gregorian::month_iterator::value_type&
>
{
   friend class boost::iterator_core_access;
public:
   month_iterator() : m_it(boost::gregorian::date(boost::gregorian::not_a_date_time)) {}
   explicit month_iterator(const boost::gregorian::month_iterator& pos) :
      m_it(pos)
   {
      receive_value();
   }
   reference dereference() const
   {
      return *value;
   }
   bool equal(const month_iterator& rhs) const
   {
      return *value >= *rhs.value;
   }
   void increment()
   {
      ++m_it;
      *this = month_iterator(m_it);
   }
private:
   void receive_value()
   {
      value.reset(new value_type(*m_it));
   }
   boost::gregorian::month_iterator m_it;
   boost::shared_ptr<value_type> value;
};

int main()
{
   boost::gregorian::month_iterator start(boost::gregorian::date(1901,1,1),1);
   boost::gregorian::month_iterator end(boost::gregorian::date(2000,12,31));

   month_iterator p(start), e(end);
   const int result = std::count_if(p, e, [](const month_iterator::value_type& v)
   {
      return v.day_of_week() == 0;
   });
   std::cout << result << std::endl;
}
于 2013-04-18T10:48:38.513 回答