472,119 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

vector: erase() and rbegin()

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 we have to write v.erase(v.end() - 1)? Or there exists something else?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #1
10 4362

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2u*************@uni-berlin.de...
vector<int> v;

erase() requires (as input parameter) and returns vector<int>::iterator,
rbegin() returnsvectorintreverse_iterator.

So, a compiler doesn't accept v.erase(v.rbegin()).
Do we have to writ
ev.eraseOrthereexistssomethingelse


reverse_iterators have a member function called base() which returns the
equivalent iterator. Use that.

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message news:2u*************@uni-berlin.de...

"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2u*************@uni-berlin.de...
vector<int> v;

erase() requires (as input parameter) and returns vector<int>::iterator,
rbegin() returnsvectorintreverse_iterator.

So, a compiler doesn't accept v.erase(v.rbegin()).
Do we have to writ
ev.eraseOrthereexistssomethingelse


reverse_iterators have a member function called base() which returns the
equivalent iterator. Use that.

john


I have got

assertion "assert ((*v.rbegin()) == (*v.rbegin().base()))" failed
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #3
John Harrison wrote:
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2u*************@uni-berlin.de...
vector<int> v;

erase() requires (as input parameter) and returns vector<int>::iterator,
rbegin() returnsvectorintreverse_iterator.

So, a compiler doesn't accept v.erase(v.rbegin()).
Do we have to writ
ev.eraseOrthereexistssomethingelse

reverse_iterators have a member function called base() which returns the
equivalent iterator. Use that.

john

From TC++PL 3:
"A reverse_iterator is implemented using an iterator called current.
That iterator can (only) point to the elements of its sequence plus its
one-past-the-end element. However, the reverse_iterator’s
one-past-the-end element is the original sequence’s (inaccessible)
one-before-the-beginning element.

Thus, to avoid access violations, current points to the element after
the one the reverse_iterator refers to. This implies that * returns the
value *(current-1) and that ++ is implemented using -- on current".

In other words the base() of reverse_iterator returns current, which is
one after the element that reverse_iterator points, under the regular
iterator point of view.
So if you want to point to the same element with reverse_iterator,
provided that reverse_iterator does not point at "one past the end"
element of its view, you can do for example:
vector<int>::reverse_iterator rp = whatever;
// Not to be used for a reverse_iterator pointing to "one past the end"
// element under its point of view.
vector<int>::iterator p = rp.base()-1;

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
Alex Vinokur wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message news:2u*************@uni-berlin.de...

I have got

assertion "assert ((*v.rbegin()) == (*v.rbegin().base()))" failed


That's undefined. Try "assert (v.rbegin ().base () == v.end ())". For a
reverse iterator pointing to an element of a collection, the base
iterator points to the following element (in the forward sequence). If j
is a reverse iterator that points to an element of a collection then
"j.base () - 1" gives a forward iterator pointing to the same element
(actually, you can only do arithmetic like this if the base iterator is
a random access iterator, but that's beside the point). Be careful
though, because if j is a reverse iterator that points one-past-the-end
(of the reverse sequence), i.e. if (j == v.rend ()), then j.base ()
points to the first element of the forward sequence, so "j.base () - 1"
is undefined.
Jul 22 '05 #5
How about the pop_back function?
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #6
>Try "assert (v.rbegin ().base () == v.end ())".

Isn't that always going to be true?
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #7
DaKoadMunky wrote:
Try "assert (v.rbegin ().base () == v.end ())".

Isn't that always going to be true?


Yes. That's what makes it an assertion.

--
Regards,
Buster
Jul 22 '05 #8
DaKoadMunky wrote:
How about the pop_back function?


The pop_back member function doesn't return a value. It is unlikely to
be of much use here. Did you mean the back member function?

--
Regards,
Buster
Jul 22 '05 #9
Buster wrote:
DaKoadMunky wrote:
How about the pop_back function?

The pop_back member function doesn't return a value. It is unlikely to
be of much use here. Did you mean the back member function?


I'm sorry, I missed your point. Yes, pop_back is a good alternative
approach to the OP's problem.

--
Regards,
Buster
Jul 22 '05 #10
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:2u*************@uni-berlin.de...
So, a compiler doesn't accept v.erase(v.rbegin()).
Do we have to write v.erase(v.end() - 1)? Or there exists something
else?


How about v.pop_back()?
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marc Schellens | last post: by
9 posts views Thread by BCC | last post: by
3 posts views Thread by Jonathan | last post: by
7 posts views Thread by William Payne | last post: by
11 posts views Thread by Richard Thompson | last post: by
17 posts views Thread by Michael Hopkins | last post: by
14 posts views Thread by cayblood | last post: by
7 posts views Thread by JH Programmer | last post: by
2 posts views Thread by Angus | last post: by

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.