473,499 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I erase() using a reverse iterator?

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

Jan 25 '07 #1
15 25415

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.

Jan 25 '07 #2
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
Jan 25 '07 #3


On 25 Jan, 11:39, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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

Jan 25 '07 #4
Boltar wrote:
>

On 25 Jan, 11:39, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>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
Jan 25 '07 #5
Boltar wrote:
>
On 25 Jan, 11:39, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>>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 ?
Jan 25 '07 #6


On 25 Jan, 12:16, Gianni Mariani <gi3nos...@mariani.wswrote:
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

Jan 25 '07 #7


On 25 Jan, 12:04, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>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

Jan 25 '07 #8
Boltar wrote:
>
On 25 Jan, 12:16, Gianni Mariani <gi3nos...@mariani.wswrote:
>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
cl*******@gmail.com
Jan 25 '07 #9
Boltar wrote:
>
On 25 Jan, 12:04, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>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)
Jan 25 '07 #10


On 25 Jan, 13:58, "Clark S. Cox III" <clarkc...@gmail.comwrote:
>Then what should rend() correspond to?
I wouldn't expect to correspond to anything, its not a valid position
in the container.

B2003

Jan 25 '07 #11


On 25 Jan, 14:23, Pete Becker <p...@versatilecoding.comwrote:
equals last:

while (first != last)
do_something(first++);
If your "last" variable really is the last entry and not the end()
value then it won't call the do_something() method for the last one. I
think possibly the naming was wrong with thes methods since begin()
really does return the beginning position of the list (if there is one)
but end() does not return the end position, it returns a value that
signifies you've already gone past the final valid position, which is
not the same thing at all. Its the difference between fgetc() returning
the final character in a file and returning EOF.
>
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.
I don't see why , so long as the begin() and end() methods are
consistent within the iterator type.

B2003

Jan 25 '07 #12
Boltar <bo********@yahoo.co.ukwrote:
On 25 Jan, 11:39, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>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....
Maybe this article can shed some light on the subject:
http://www.ddj.com/dept/cpp/184401406

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 25 '07 #13
Boltar wrote:
>
On 25 Jan, 14:23, Pete Becker <p...@versatilecoding.comwrote:
>equals last:

while (first != last)
do_something(*first++);
I added the missing * in the line above.
>
If your "last" variable really is the last entry and not the end()
value then it won't call the do_something() method for the last one.
The algorithm does not call do_something when first == last. That's
fundamental. And it's the right behavior when you call an algorithm with
a pair of iterators from a container, whether that pair is obtained by
calling begin() and end() or by calling rbegin() and rend().
I
think possibly the naming was wrong with thes methods since begin()
really does return the beginning position of the list (if there is one)
but end() does not return the end position, it returns a value that
signifies you've already gone past the final valid position,
They return iterators that mark the beginning and the end of the
sequence that's managed by the container. Again: a sequence is
designated by a pair of iterators. The first iterator points to the
first element in the sequence and the second iteraotr points past the
end of the sequence. It doesn't matter where the sequence comes from:
that's what they have to be.
which is
not the same thing at all. Its the difference between fgetc() returning
the final character in a file and returning EOF.
Not really. begin() and end() [and rbegin() and rend()] return
iterators. To check whether you're at the end of a sequence you compare
the iterators for equality.
>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.

I don't see why , so long as the begin() and end() methods are
consistent within the iterator type.
Iterator types do not have begin() and end() methods, so I don't know
what you're trying to say here.

The reason that rbegin() and rend() hold the iterator that points to the
element that precedes the element that they point at is simply that
rend() can't be implemented any other way. Using vector, for example,
you can't have an iterator that points to the element before the first
element in the contained array. So rend() creates an iterator that
points to the first element.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jan 25 '07 #14
Boltar wrote:
>
On 25 Jan, 14:23, Pete Becker <p...@versatilecoding.comwrote:
>equals last:

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

If your "last" variable really is the last entry and not the end()
value then it won't call the do_something() method for the last one.
That's the point. Without that convention, there would be no way to
indicate an empty sequence using iterator notation.
I think possibly the naming was wrong with thes methods since begin()
really does return the beginning position of the list (if there is
one) but end() does not return the end position,
But it *does* return the end position (as distinct from the last element
*in* the sequence). Perhaps visualizing it will help(requires a
mono-spaced font):

In a sequence of 5 objects (that may or may not be in a container):

+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 |
+---+---+---+---+---+
^ ^
| |
begin end
rend rbegin
it returns a value that signifies you've already gone past the final
valid position, which is not the same thing at all. Its the
difference between fgetc() returning the final character in a file
and returning EOF.
>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.

I don't see why , so long as the begin() and end() methods are
consistent within the iterator type.
Because, under your suggestion that reverse iterators and the underlying
iterators dereference to the same element, dereferencing the iterator
returned by rbegin() wouldn't be valid (as it would be like
dereferencing the iterator returned by end()).

Every algorithm would have to be rewritten to take this into account
when dealing with reverse iterators.
--
Clark S. Cox III
cl*******@gmail.com
Jan 25 '07 #15
Boltar wrote:
>
On 25 Jan, 13:58, "Clark S. Cox III" <clarkc...@gmail.comwrote:
>>Then what should rend() correspond to?


I wouldn't expect to correspond to anything, its not a valid position
in the container.
OK. Is it undefined to call base() on rend() ?
Jan 25 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3360
by: BCC | last post by:
I have the following code, where good_list is a vector of CUnits: int high_cutoff = 10; vector<CUnit>::iterator it; for (it = good_list.end(); it != good_list.begin(); --it) { CUnit* ccu = it;...
5
2743
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
10
4550
by: Alex Vinokur | last post by:
vector<int> v; erase() requires (as input parameter) and returns vector<int>::iterator, rbegin() returns vector<int>::reverse_iterator. So, a compiler doesn't accept v.erase(v.rbegin()). Do...
8
6154
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
3
6081
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently...
16
3564
by: Frank Neuhaus | last post by:
Hi I have some std list, I'd like to traverse. During the traversal, I want to conditionally delete some objects. My code for that is like this right now: for (std::list<myStruct>::iterator...
6
11877
by: catphive.lists | last post by:
Is there a way to call erase(iter) on a list without invalidating the iterator? Can I make a copy of an iterator and then move forward the original without moving the copy? I'm aware of the...
4
4217
by: sks | last post by:
I have a question regarding std::multimap/iterators. At the SGI website, it says "Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that...
3
7412
by: noone | last post by:
string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator t=thequeue.rend(); while (i!=thequeue.rend()) { if...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.