
"Neal D. Becker" <nbecker@hns.com> writes:
I am trying to learn about the new iterator adaptors. I reviewed the docs and examples, but I didn't really find much that would qualify as the "hello world" simple example.
Seriously? How much simpler can an iterator be than the node_iterator?
I put some test together. Perhaps it might either A) serve as a intro to someone else or B) some of you more knowlegeable can tell me how to do better.
Please don't take my criticisms below as lack of appreciation. I'm sure more examples would be very helpful.
#include <boost/iterator/iterator_facade.hpp> #include <vector> #include <algorithm> #include <iostream> #include <boost/array.hpp>
using namespace boost; using namespace std;
template<class Iter> class noop_iterator : public iterator_facade<noop_iterator<Iter>, typename iterator_traits<Iter>::value_type, forward_traversal_tag> { public:
typedef typename iterator_traits<Iter>::value_type value_t;
explicit noop_iterator (Iter i) : iter (i) {}
private:
friend class iterator_core_access;
void increment() { iter++; }
value_t& dereference() const { return *iter; }
Iter iter;
bool equal (noop_iterator const& other) const { return other.iter == iter; } };
I can't understand why: 1. anyone would use iterator_facade for this, since iterator_adaptor handles the increment and dereference forwarding for you 2. Why anyone would want to do this in the first place, since it just produces an equivalent or less-capable iterator (and it won't work with generalized input iterators that don't return references from operator*) IMO examples should have some real-world applicability, or people just go "what's the point?" They should also come with some comments that describe what the example is illustrating.
template<class Cont> class zippy_iterator : public iterator_facade<zippy_iterator<Cont>, typename iterator_traits<typename Cont::value_type>::value_type, forward_traversal_tag> { public:
typedef typename Cont::value_type iter_t; typedef typename iterator_traits<iter_t>::value_type value_t;
explicit zippy_iterator (Cont c) : cont (c), count (0) {}
value_t& dereference() const { return *cont[count % cont.size()]; }
void increment() { cont[count % cont.size()]++; count++; }
bool equal (zippy_iterator const& other) const { for (int i = 0; i < cont.size(); i++) if (cont[i] != other.cont[i]) return false;
return true; }
private:
Cont cont;
int count;
};
Took me about 10 minutes of staring to figure out what zippy_iterator did. A couple of sentences would've helped. Is "zippy" really a better term than "interlace" or something?
int main () { vector<int> v (10); typedef vector<int>::iterator vit;
copy (noop_iterator<vit> (v.begin()), noop_iterator<vit> (v.end()), ostream_iterator<int> (cout, "\n"));
array<vit, 2> a;
vector<int> v0 (10, 1); a[0] = v0.begin(); vector<int> v1 (10); a[1] = v1.begin();
array<vit, 2> b; b[0] = v0.end(); b[1] = v1.end();
typedef array<vit, 2> a_t;
What's the typedef for?
zippy_iterator<a_t> z1 (a); zippy_iterator<a_t> z2 (b);
copy (z1, z2, ostream_iterator<int> (cout, " ")); } _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Dave Abrahams Boost Consulting www.boost-consulting.com