Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-07-26 11:54:10


-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com

----- Original Message -----
From: "Jeremy Siek" <jsiek_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, July 26, 2002 12:16 PM
Subject: Re: [Boost-Users] Re: [boost] Iterator Adaptor Poll

> On Fri, 26 Jul 2002, David Abrahams wrote:
> dave>
> dave> Thanks! Now where's that line_iterator?
> dave>
>
> Right here :)

Do you think this makes a good "hello, world"?

It stores state in the Policies object, and returns an internal reference.
Both of these features make it a bit too complicated for a very basic
introduction, IMO.

-Dave

> // Example adapted from page 4 of Matthew Austern's
> // "Generic Programming and the STL".
>
> #include <iostream>
> #include <string>
> #include <cassert>
> #include <boost/iterator_adaptors.hpp>
> #include <boost/cstdlib.hpp>
>
> class line_iterator_policies : public boost::default_iterator_policies
> {
> std::istream* in; // rationale for pointer: if we did
> reference
> // instead, then iterator could not be
> default
> // constructible.
> std::string line;
> public:
> line_iterator_policies() : in(0) { } // always need a default
> constructor
> line_iterator_policies(std::istream& in) : in(&in) { }
>
> void initialize(bool& at_end) {
> if (in != 0)
> read(at_end);
> else
> at_end = true;
> }
>
> template <typename IteratorAdaptor>
> typename IteratorAdaptor::reference
> dereference(const IteratorAdaptor& x) const
> { return line; }
>
> template <typename IteratorAdaptor>
> void increment(IteratorAdaptor& x) { read(x.base()); }
> private:
> void read(bool& at_end) { assert(in != 0);
> if (*in)
> std::getline(*in, line);
> if (*in)
> at_end = true;
> else
> at_end = false;
> }
> };
>
>
> typedef boost::iterator_adaptor<bool, // for at_end
> line_iterator_policies,
> boost::value_type_is<std::string>,
> boost::reference_is<std::string>,
> boost::pointer_is<std::string*>,
> boost::difference_type_is<std::ptrdiff_t>,
> boost::iterator_category_is<std::input_iterator_tag>
> > line_iterator;
>
> line_iterator make_line_iterator(std::istream& in) {
> bool at_end;
> line_iterator_policies p(in);
> return line_iterator(at_end, p);
> }
>
> int main()
> {
> line_iterator iter = make_line_iterator(std::cin), eof_iter;
> std::vector<std::string> vec(iter, eof_iter);
> std::sort(vec.begin(), vec.end());
> std::copy(vec.begin(), vec.end(),
> std::ostream_iterator<std::string>(std::cout, "\n"));
> return boost::exit_success;
> };
>
>
>
> ----------------------------------------------------------------------
> Jeremy Siek http://php.indiana.edu/~jsiek/
> Ph.D. Student, Indiana Univ. B'ton email: jsiek_at_[hidden]
> C++ Booster (http://www.boost.org) office phone: (812) 855-3608
> ----------------------------------------------------------------------
>
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk