vector: erase() and rbegin()

A

Alex Vinokur

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?
 
J

John Harrison

Alex Vinokur said:
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
 
A

Alex Vinokur

John Harrison said:
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
 
I

Ioannis Vranos

John said:
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;
 
B

Buster

Alex said:
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.
 
D

DaKoadMunky

How about the pop_back function?
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
 
D

DaKoadMunky

Try "assert (v.rbegin ().base () == v.end ())".

Isn't that always going to be true?
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
 
A

Andrew Koenig

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()?
 
B

Buster

DaKoadMunky said:
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?
 
B

Buster

Buster said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top