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 15 24957
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.
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
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
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
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 ?
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
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
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
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)
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
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
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
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)
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
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() ? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;...
|
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...
|
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...
|
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)
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
|
by: F22F35 |
last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
| |