Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > custom iterator

Reply
Thread Tools

custom iterator

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-13-2005
* Mike Wahler:
> But I believe the type is 'std:trdiff_t'


Typo, thanks.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-14-2005
wrote:
> thanks for the replies, they were all helpful.
>
> I've a question about iterator behaviour. if I have
>
> difference_type res = iter1 - iter2
>
> the SGI website says that the precondition is that iter1 is "reachable
> from iter2 or vice-versa, or both".
> that includes past-the-end iterators too, doesn't it, for either iter1
> or iter2. I mean if there are 10 iterms in a vector and I have iter1
> and iter2 such that iter1 points to vector[3] and iter2 points to
> past-the-end , then iter2-iter1 is still valid, isn't it? it's going to
> return an iterator thats pointing to element[7] in the vector. yes?


This is clearly a mental block so I'll say it loud.

THE RETURN VALUE WHEN YOU SUBTRACT TWO ITERATORS IS AN

***INTEGER***

Got that? Once you have everything else will fall into place.

john
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-14-2005
wrote:
> one other thing... I have the iterators setup and I can now perform
> things like;
>
> myIter i1 = wrapper.begin();
> myIter i2 = wrapper.end();
>
> std::swap(i1, i2 );
>
> I can also call std::sort(i1 , i2);
>
> the above swap works fine and when I output the container contents, its
> contents are correctly ordered.
>
> however if I try to do something like;;
>
> std::sort(i1, i2 -1 );
>
> the (borland) compiler complains that;
>
> [C++ Error] iterator.cpp(285): E2285 Could not find a match for
> 'swap<_Tp,_Alloc>(myIter,myIter)'


Can't see what the problem is there, how about posting line 285 from
iterator.cpp

>
> I have defined operator- as
>
> // difference
> int operator-(const contIter& rhs) const
> {
> return abs(m_index - rhs.m_index);
> }


Are you sure that's right? Surely should be

int operator-(const contIter& rhs) const
{
return m_index - rhs.m_index;
}

Your code will always return a positive value, what if rhs is further
advanced than *this?

>
> contIter operator-(int i) const
> {
> return contIter(m_container, m_index-i);
> }
>
>
>
>
> I\m obviously missing something but can't see what. any ideas?
>
>
> thanks much again
>
> GrahamO
>


john
 
Reply With Quote
 
Grahamo@nospam.com
Guest
Posts: n/a
 
      09-14-2005
thanks Clarke/John,

I've cleared up the operator- business now (combination of typos and
errors on my behalf), thanks for your help.

I have painted "THE RETURN VALUE WHEN YOU SUBTRACT TWO ITERATORS IS AN
***INTEGER*** " onto the ceiling of my bedroom wall .. well not quite
but I've commited it to memory at least

Likewise the abs problem is corrected.

Everything is working nicely now, I can std::swap, std::sort, etc.
using my custom iterators.

thanks for the replies. G

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57