Boost logo

Boost Users :

From: Duane Murphy (duanemurphy_at_[hidden])
Date: 2002-07-25 16:35:16


--- At Thu, 25 Jul 2002 16:59:36 -0400, David Abrahams wrote:

>Hi Everyone,
>
>Have you tried to use iterator adaptors and found the documentation
>wanting? I have recently updated the documentation in the CVS:
>http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/
>utility/iterator_adaptors.htm?rev=HEAD

Great. I'll have to read up.

>However, enough people have had problems approaching this library that I
>thought it would be useful to ask about what would make a gentle
>introductory example. Beman recently sent me the enclosed, but it seems to
>define such an oddball toy iterator that I'm not sure it would be a very
>good motivating example. I'm asking for input, especially from anyone who
>has "gotten over the hump" so to speak, with iterator adaptors.

I would prefer a more practical example. I cant even really tell what
this one does without careful study.

I would suggest something out of the standard C library that would need
iterators. I guess its not really standard, but I still think the
directory iterator (iterate over the files in a directory) is a good
example. Lots of people have written similar code.

At least something along those lines.

I guess my presonal focus is adapting things that dont have iterators but
have iterator semantics. With the use of bind and the use of function
objects in algorithms, I dont see a lot of use of iterator_adaptor for
modifying the affects of an iterator.

The other area that I still get confused on is how to support both const
and non-const iterators. I'll check the docs to see if that has been made
any clearer.

>----- Original Message -----
>From: "Beman Dawes" <bdawes_at_[hidden]>
>To: "Dave Abrahams" <david.abrahams_at_[hidden]>
>Cc: "Jeremy Siek" <jsiek_at_[hidden]>
>Sent: Thursday, July 25, 2002 3:21 PM
>Subject: Iterator adaptor example
>
>
>> Dave,
>>
>> Thanks for fixing the docs. I fixed and committed to CVS one more change
>> of BaseType -> Base.
>>
>> I found I needed an even more basic example than anything in the docs to
>> play with, particularly for creating an iterator from scratch, and put
>> together the attached.
>>
>> It is very simple, and does not demonstrate advanced features, but it
>> answered newbie questions I had in a way that only running code,
>modifying
>> it, and running again can do. (Not to mention fixing all the errors I
>made
>> before it would compile!)
>>
>> Feel free to include it in the distribution if you think it would help
>> others. Needs a better name of course.
>>
>> --Beman
>
>
> ----------
>
>// Really dumb iterator_adaptor example program
>-----------------------------//
>
>// (C) Copyright Beman Dawes 2002. Permission to copy, use, modify, sell and
>// distribute this software is granted provided this copyright notice appears
>// in all copies. This software is provided "as is" without express or
implied
>// warranty, and with no claim as to its suitability for any purpose.
>
>#include <boost/iterator_adaptors.hpp>
>#include <string>
>#include <iostream>
>
>class my_base
>{
>public:
> std::string name;
> my_base() : name( "foo" ) {}
> void operator++() { name = "bar"; }
> bool operator==( const my_base & rhs ) const { return name == rhs.name; }
>};
>
>// if my_base had "const std::string & operator*() const { return name; }"
>// then custom policies would not be required.
>class my_policies : public boost::default_iterator_policies
>{
>public:
> template <class IteratorAdaptor>
> typename IteratorAdaptor::reference dereference(const IteratorAdaptor&
>x) const
> { return x.base().name; }
>};
>
>typedef boost::iterator_adaptor<
> my_base,
> my_policies, // default_iterator_policies would do if my_base::op*
>were present
> const std::string,
> const std::string &,
> const std::string *,
> std::forward_iterator_tag,
> std::string::difference_type > my_iterator;
>
>int main()
>{
> my_iterator itr;
> my_iterator::value_type str( "hello, iterator\n" ); //verify value_type
>present
> std::cout << str;
> std::cout << *itr << std::endl; // foo
> std::cout << *++itr << std::endl; // bar
> std::cout << (itr == my_iterator() ? "bingo!" : "oops!" ) << std::endl;
>// oops
> std::cout << (itr == ++my_iterator() ? "bingo!" : "oops!" ) <<
>std::endl; // bingo
> return 0;
>}

 ...Duane

-- 
"If tyranny and oppression come to this land, it will be in the
guise of fighting a foreign enemy."              - James Madison

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net