Wanted: Help implementing minimal iterator

  • Follow


I've been google searching for help and reading Josuttis's book "The
Standard C++ Library" and cannot find a simple recipie (cookbook approach)
for recreating a minimal iterator.

Here is what I have so far that works:

//std::ostream_iterator<ClusterPseudoContainer<DUMMY002::vector_bool_t>::ite
rator > out(oss," ");

//std::copy( ccDirty.begin(), ccDirty.end(), out);

for(ClusterPseudoContainer<DUMMY002::vector_bool_t>::iterator ii =
ccDirty.begin(); ii != ccDirty.end(); ++ii)

oss << " " << *ii;


I've defined the operator++ for the inner const_iterator and iterator
classes of ClusterPseudoContainer. operator* returns an instance of class
Cluster, another inner class. Operator<< is defined for class Cluster.

I would like to use the above commented code. The commented code does not
work.

Microsoft MSVC7 says
"c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xutility(1022): error C2679: binary '=' : no operator found
which takes a right-hand operand of type
'ClusterPseudoContainer<ContainerType>::Cluster' (or there is no acceptable
conversion)
         with
         [
             ContainerType=std::vector<bool,std::allocator<bool>>
         ]
"

  What is necessary to make the commented code work? g++ v3.2 error messages
are similar.
Thanks,
Siegfried




      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Siegfried 4/8/2006 10:06:48 AM

"Siegfried Heintze" <siegfried@heintze.com> writes:

> I've been google searching for help and reading Josuttis's book "The
> Standard C++ Library" and cannot find a simple recipie (cookbook approach)
> for recreating a minimal iterator.


Try
http://www.boost.org/libs/iterator/doc/index.html#iterator-facade-and-adaptor

It's very easy for even experienced C++ programmers to create
incorrect "iterators" unless they use these libraries.

HTH,
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply David 4/9/2006 8:58:25 AM


In article <44367db0$1@mamba.>, Siegfried Heintze
<siegfried@heintze.com> wrote:

> I've been google searching for help and reading Josuttis's book "The
> Standard C++ Library" and cannot find a simple recipie (cookbook approach)
> for recreating a minimal iterator.
>
> Here is what I have so far that works:
>
> //std::ostream_iterator<ClusterPseudoContainer<DUMMY002::vector_bool_t>::ite
> rator > out(oss," ");
>
> //std::copy( ccDirty.begin(), ccDirty.end(), out);
>
> for(ClusterPseudoContainer<DUMMY002::vector_bool_t>::iterator ii =
> ccDirty.begin(); ii != ccDirty.end(); ++ii)
>
> oss << " " << *ii;

    The commented out code and the explicit loop are different.  Since
the error refers to operator '=' . Note that
std::ostream_iterator<T,...> has an std::ostream_iterator<T,...> &
operator = (const T &)  and operator *() is an essential no op
returning
a reference to the iterator... so that *out = x  reduces to some_stream
<< x << trailing C string .  But you don't get there because you have
no conversion from YourContainer::iterator and
YourContainer::value_type.    The required parameter to
ostream_iterator is the type being output not an iterator.

std::copy is a loop that has   *begin = *out as a part of its body. The
this is where the operator '=' stuff is coming from. So to fix this
change the template argument of ostream_iterator accordingly.

If you really want to impliment custom iterators you should check out
boost's iterator library as it make implimentation of custom iterators
easier.   But this looks like a misunderstanding of the template
arguments for std::ostream_iterator<T,Ch=char,Tr=std::allocator<Ch> > ,

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply cbarron413 (16) 4/9/2006 9:01:44 AM

2 Replies
97 Views

(page loaded in 0.085 seconds)

Similiar Articles:













7/26/2012 2:27:52 AM


Reply: