我正在尝试itertools.tee
为 Boost::Range 编写 Python 的 C++ 版本(如此处所示)。这是我的第一次尝试:
template<typename R>
class tee_iterator : std::iterator<std::forward_iterator_tag, typename boost::range_value<R>::type>
{
public:
typedef typename boost::range_value<R>::type T;
typedef std::list<T> tee_queue;
typedef std::vector<tee_queue> tee_queue_collection;
tee_iterator(const R& r, tee_queue* q, tee_queue_collection* qs) :
it_(r.begin()), queue_(q), queues_(qs) {}
tee_iterator(const R& r) : it_(r.end()), queue_(NULL), queues_(NULL) {}
T& operator*() const { return current_; }
tee_iterator& operator++()
{
if (queue_->empty()) {
++it_;
for (auto q : queues_) {
q->push_back(*it_);
}
}
current_ = queue_->front();
queue_->pop_front();
return *this;
}
bool operator==(tee_iterator const& o) const { return it_ == o.it_; }
bool operator!=(tee_iterator const& o) const { return !(*this == o); }
private:
typedef typename boost::range_iterator<const R>::type const_iterator;
const_iterator it_;
tee_queue* queue_;
tee_queue_collection* queues_;
T current_;
};
template<typename R>
using tee_range = boost::iterator_range<tee_iterator<R> >;
template<typename R>
std::list<tee_range<R> > tee(const R& r, int n)
{
typedef typename boost::range_value<R>::type T;
typedef std::list<T> tee_queue;
typedef std::vector<tee_queue> tee_queue_collection;
tee_queue_collection queues(n);
std::list<tee_range<R> > ranges;
for (int i = 0; i < n; ++i) {
tee_range<R> t = { tee_iterator<R>(r, &queues[i], &queues), tee_iterator<R>(r) };
ranges.push_back(t);
}
return ranges;
}
但是一旦我尝试使用它:
int main(int argc, char* argv[])
{
std::list<int> l;
for (int i = 0; i < 10; ++i) {
l.push_back(i);
}
auto t = tee(l, 3);
}
它吹在我的脸上,抱怨(和其他人)失踪value_type
。boost::detail::iterator_traits<tee_iterator<std::list<int, std::allocator<int> > > >
我错过了什么?tee_iterator
做孩子std::iterator
还不够吗?