Simple examples for new iterator adaptors

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. 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.

Hi Neal, That's a fun example. Why don't you put a copyright on it and we'll add it to the example directory. Cheers, Jeremy On Feb 3, 2004, at 4:00 PM, Neal D. Becker wrote:
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.
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.
<TestZip.cc>_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Jeremy Siek <jsiek@osl.iu.edu> http://www.osl.iu.edu.edu/~jsiek Ph.D. Student, Indiana University Bloomington C++ Booster (http://www.boost.org) Office phone: (812) 856-1820 _______________________________________________

Here is a cleaner and better version of my 2 examples. I also have a couple of questions, maybe someone can help me to improve these: Question 1: As a convenience in TestZip.cc, I made the function: template<typename T> inline array<T,2> make_array_2 (T t1, T t2); Is there a resonable way to make a function make_array_N? Question 2: In zippy_iterator.H I needed to add const_casts or else I got errors on std::distance. Is there a better approach?

"Neal D. Becker" <nbecker@hns.com> writes:
Here is a cleaner and better version of my 2 examples.
I didn't take too long to look at them, but some of the lines in zippy_iterator are really long. I think you could improve readability a lot by wrapping and indenting.
I also have a couple of questions, maybe someone can help me to improve these:
Question 1: As a convenience in TestZip.cc, I made the function: template<typename T> inline array<T,2> make_array_2 (T t1, T t2);
Is there a resonable way to make a function make_array_N?
I guess I don't understand what you mean by "reasonable". Is there something wrong with what you did?
Question 2:
In zippy_iterator.H I needed to add const_casts or else I got errors on std::distance. Is there a better approach?
Well, const_cast doesn't really work there as you'll see if you use, e.g., deques of iterators instead of boost::array's of iterators. But I can't see why distance would cause you any trouble without the const_casts in the first place. What errors were you seeing? BTW, thanks for working on these examples. More examples are sure to help our users! -- Dave Abrahams Boost Consulting www.boost-consulting.com

"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
participants (3)
-
David Abrahams
-
Jeremy Siek
-
Neal D. Becker