|
Boost Users : |
From: David Abrahams (dave_at_[hidden])
Date: 2002-07-25 15:59:36
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
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.
TIA,
Dave
---------------------------------
David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com
----- 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;
}
[Non-text portions of this message have been removed]
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