Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I erase() using a reverse iterator?

Reply
Thread Tools

How can I erase() using a reverse iterator?

 
 
Boltar
Guest
Posts: n/a
 
      01-25-2007
Hi

I'm going through an STL list container using a reverse iterator but it
seems the erase() method only accepts ordinary iterators. Is there a
similar method that will accept reverse iterators or alternatively is
there a way to convert a reverse iterator to a normal one?

Thanks for any help

B2003

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-25-2007

Boltar napsal:
> Hi
>
> I'm going through an STL list container using a reverse iterator but it
> seems the erase() method only accepts ordinary iterators. Is there a
> similar method that will accept reverse iterators or alternatively is
> there a way to convert a reverse iterator to a normal one?
>
> Thanks for any help
>
> B2003


I do not know which type of container are you using. Anyway I think you
are not erasing all items in one step. So iterators will become
invalid. You should use

erase(container.rbegin(), container.rend());

Or simillar and it should work.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-25-2007
Boltar wrote:

> I'm going through an STL list container using a reverse iterator but it
> seems the erase() method only accepts ordinary iterators. Is there a
> similar method that will accept reverse iterators or alternatively is
> there a way to convert a reverse iterator to a normal one?


The reverse_iterator has a member function base() that will give you the
value of the underlying iterator. Beware, however, that the underlying
iterator points to a different element. It's off by one, and the precise
relation ship is given in the standard [24.4.1/1] as:

&*(reverse_iterator(i)) == &*(i - 1)


Best

Kai-Uwe Bux
 
Reply With Quote
 
Boltar
Guest
Posts: n/a
 
      01-25-2007


On 25 Jan, 11:39, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> value of the underlying iterator. Beware, however, that the underlying
> iterator points to a different element. It's off by one, and the precise
> relation ship is given in the standard [24.4.1/1] as:
>
> &*(reverse_iterator(i)) == &*(i - 1)


Thanks. That sounds like another triumph of committee decision making.
Why on earth did they think having it off by one would be useful? Ah
well....

B2003

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-25-2007
Boltar wrote:

>
>
> On 25 Jan, 11:39, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> value of the underlying iterator. Beware, however, that the underlying
>> iterator points to a different element. It's off by one, and the precise
>> relation ship is given in the standard [24.4.1/1] as:
>>
>> &*(reverse_iterator(i)) == &*(i - 1)

>
> Thanks. That sounds like another triumph of committee decision making.
> Why on earth did they think having it off by one would be useful?


Well, this way, rbegin() corresponds to end() and rend() corresponds to
begin(). If you had rbegin() be end()-1, what would rend() be? Before you
answer begin()-1, you should consider the fact that that value simply does
not exist (not even for built-in arrays).


Best

Kai-Uwe Bux
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-25-2007
Boltar wrote:
>
> On 25 Jan, 11:39, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>
>>value of the underlying iterator. Beware, however, that the underlying
>>iterator points to a different element. It's off by one, and the precise
>>relation ship is given in the standard [24.4.1/1] as:
>>
>> &*(reverse_iterator(i)) == &*(i - 1)

>
>
> Thanks. That sounds like another triumph of committee decision making.
> Why on earth did they think having it off by one would be useful? Ah
> well....


What would your alternative definition be ?
 
Reply With Quote
 
Boltar
Guest
Posts: n/a
 
      01-25-2007


On 25 Jan, 12:16, Gianni Mariani <(E-Mail Removed)> wrote:
> What would your alternative definition be ?


Make them the same. Why would you want anything else? If I want to get
an iterator from a reverse iterator why would I want them to point at
different container entries? Its illogical.

B2003

 
Reply With Quote
 
Boltar
Guest
Posts: n/a
 
      01-25-2007


On 25 Jan, 12:04, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>Well, this way, rbegin() corresponds to end() and rend() corresponds to
> begin().


Hardly useful and also logically incorrect since it implies end() is
the last element in the container which it isn't. Far more useful to
have the iterators correspond to each other IMO.

B2003

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-25-2007
Boltar wrote:
>
> On 25 Jan, 12:16, Gianni Mariani <(E-Mail Removed)> wrote:
>> What would your alternative definition be ?

>
> Make them the same. Why would you want anything else? If I want to get
> an iterator from a reverse iterator why would I want them to point at
> different container entries? Its illogical.


Then what should rend() correspond to?


--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-25-2007
Boltar wrote:
>
> On 25 Jan, 12:04, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> Well, this way, rbegin() corresponds to end() and rend() corresponds to
>> begin().

>
> Hardly useful


On the contrary: it's exactly what you need when you pass reverse
iterators to algorithms.

> and also logically incorrect since it implies end() is
> the last element in the container which it isn't.


It implies that cont.end() returns an iterator that is past the end of
the sequence held in the container, and that cont.rend() returns an
iterator that is past the end of the reverse sequence held in the container.

> Far more useful to
> have the iterators correspond to each other IMO.
>


You're focusing too much on containers. The fundamental concept in STL
is the range: a pair of iterators [first, last) that describes a
sequence of elements. The iterator first points to the first element in
the sequence. The iterator last points past the end of the sequence. You
visit all the elements in that range by incrementing first until it
equals last:

while (first != last)
do_something(first++);

If reverse iterators worked the way you suggest, you'd have to duplicate
all of the algorithms with a different control loop to handle reverse
iterators.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Formatted MAC OSX Partition using NTFS - Need to reverse that and getdata files back... Can You Help? pspuser Computer Support 3 01-22-2008 09:27 AM
Can I reverse eng a .pyc back to .py? Steven W. Orr Python 0 02-19-2007 05:11 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM
Sony 707 - How can I attach and reverse a 50mm lens to this camera ? Coolasblu Digital Photography 1 08-08-2003 10:11 PM



Advertisments