Sure, I'm all for toys, as in "this doesn't really do everything I'd want from a real example, but it shows me how the system works". The problem I have with ia_experiment.cpp is that my first reaction was "why would anyone want an "iterator" which does *that*?" The fact that it iterates over only two elements and stores its value internally is pretty weird. I think most people who have only seen the standard iterators would have a hard time imagining that this is also an iterator. Though technically, it is an iterator, it's so different in nature from most iterators that it seems like it would be completely foreign.
What about a singly-linked-list iterator example? Wouldn't that show everything you're illustrating, but more transparently?
Why not some kind of rotation iterator. The advantage of this example is
From: "Toon Knapen"
there's a std::rotate in the STL which modifies the container whereas the rotation_iterator_adaptor can give you the same result but withouth modifying the containers. AFAICT this is a strong argument in favour of the IA.
That's neat, but we already have the permutation adaptor, and I don't think it's really a simple enough example. Beman's example just makes a simple iterator, not an adaptor, and I think that's an important feature for a "hello, world"-type introduction. -Dave ----------------------------------------------------------- David Abrahams * Boost Consulting dave@boost-consulting.com * http://www.boost-consulting.com
Here's another idea for a "hello world" example. A rewrite of Matt Austern's line_iterator example from his Generic Programming and the STL book. The line_iterator call getline() and then return a std::string from operator*. On Fri, 26 Jul 2002, David Abrahams wrote: dave> dave> That's neat, but we already have the permutation adaptor, and I dave> don't think it's really a simple enough example. Beman's example dave> just makes a simple iterator, not an adaptor, and I think that's an dave> important feature for a "hello, world"-type introduction. dave> dave> -Dave ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
From: "Jeremy Siek"
Here's another idea for a "hello world" example. A rewrite of Matt Austern's line_iterator example from his Generic Programming and the STL book. The line_iterator call getline() and then return a std::string from operator*.
On Fri, 26 Jul 2002, David Abrahams wrote: dave> dave> That's neat, but we already have the permutation adaptor, and I dave> don't think it's really a simple enough example. Beman's example dave> just makes a simple iterator, not an adaptor, and I think that's an dave> important feature for a "hello, world"-type introduction. dave> dave> -Dave
Don't you think people will find an iterator which traverses the elements of a real sequence to be more-obvious? That's why I'm suggesting the singly-linked list iterator. ----------------------------------------------------------- David Abrahams * Boost Consulting dave@boost-consulting.com * http://www.boost-consulting.com
On Fri, 26 Jul 2002, David Abrahams wrote: dave> dave> Don't you think people will find an iterator which traverses the dave> elements of a real sequence to be more-obvious? That's why I'm dave> suggesting the singly-linked list iterator. The singly-linked list example was the second thing that popped into my mind... I like that a lot too. Perhaps we should do both :) Cheers, Jeremy ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
OK, here's my (untested) singly-linked-list iterator:
typedef std::string slist_data;
struct slist_node
{
slist_node* next;
slist_data data;
};
struct slist_policies : default_iterator_policies
{
template <class IteratorAdaptor>
void increment(IteratorAdaptor& x) {
x.base() = x.base()->next;
}
template <class IteratorAdaptor>
typename IteratorAdaptor::reference dereference(IteratorAdaptor& x) {
return x.base()->data;
}
// initialize, equal covered by default policies
};
typedef iterator_adaptor
On Fri, 26 Jul 2002, David Abrahams wrote: dave> dave> Don't you think people will find an iterator which traverses the dave> elements of a real sequence to be more-obvious? That's why I'm dave> suggesting the singly-linked list iterator.
The singly-linked list example was the second thing that popped into my mind... I like that a lot too. Perhaps we should do both :)
Cheers, Jeremy
---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Looks great! BTW, I think this example will be really useful to people who
are trying to use old C data-structures inside of new C++ code.
It would be great to have a slist implementation in boost...
On Fri, 26 Jul 2002, David Abrahams wrote:
dave> OK, here's my (untested) singly-linked-list iterator:
dave>
dave> typedef std::string slist_data;
dave> struct slist_node
dave> {
dave> slist_node* next;
dave> slist_data data;
dave> };
dave>
dave> struct slist_policies : default_iterator_policies
dave> {
dave> template <class IteratorAdaptor>
dave> void increment(IteratorAdaptor& x) {
dave> x.base() = x.base()->next;
dave> }
dave>
dave> template <class IteratorAdaptor>
dave> typename IteratorAdaptor::reference dereference(IteratorAdaptor& x) {
dave> return x.base()->data;
dave> }
dave>
dave> // initialize, equal covered by default policies
dave> };
dave>
dave> typedef iterator_adaptor
At 11:10 AM 7/26/2002, David Abrahams wrote:
OK, here's my (untested) singly-linked-list iterator:
Yes! Nice! Having this would have saved me a lot of bumbling around. How about supplying slist_node with an operator++ (and eliminating slist_policies::increment), so that you are illustrating the two different possibilities for supplying a required operation. That was something that confused me initially. Another thing that still confuses me is why you need to specify Value, but can let Reference, Pointer, and Distance default. A little cometary in the docs which go with the example to explain the rationale for your choice of template parameter values would be much appreciated. --Beman
typedef std::string slist_data; struct slist_node { slist_node* next; slist_data data; };
struct slist_policies : default_iterator_policies { template <class IteratorAdaptor> void increment(IteratorAdaptor& x) { x.base() = x.base()->next; }
template <class IteratorAdaptor> typename IteratorAdaptor::reference dereference(IteratorAdaptor& x) { return x.base()->data; }
// initialize, equal covered by default policies };
typedef iterator_adaptor
std::forward_iterator_tag > slist_iterator; typedef iterator_adaptor
std::forward_iterator_tag > slist_const_iterator;
That's neat, but we already have the permutation adaptor, and I don't
At 10:15 AM 7/26/2002, David Abrahams wrote: think
it's really a simple enough example. Beman's example just makes a simple iterator, not an adaptor, and I think that's an important feature for a "hello, world"-type introduction.
Yes, that what I think is needed for the first introductory example. By the way, after getting the ia_experiment to work, I went on to write a real iterator (for iterating over the names in a path). It was very easy to write and worked right away. Having ia_experiment.cpp in front of me was very helpful. Dave's suggestion for a list iterator would probably be more understandable to more people. --Beman
participants (3)
-
Beman Dawes
-
David Abrahams
-
Jeremy Siek