473,498 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4549

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2249
by: Marc Schellens | last post by:
my dinkumware docu says, vector<...>::rbegin() returns an iterator which points just BEYOND the end of the controlled sequence. Is that true? so I cannot say: for( riter i=v.rbegin(); i !=...
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;...
3
1987
by: Jonathan | last post by:
Hey again everyone! I have another question for you guys. I am trying to erase a certain vector element based on what number the user selects. Here is what I did: case 'b' : { cout << "Please...
7
12564
by: William Payne | last post by:
(This post is related to "recent files menu"-post below. If I should have kept this in that thread, I apologise.) Hello, I have a function that adds a std::string to a std::vector. New entries are...
11
2880
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
17
3321
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
14
18563
by: cayblood | last post by:
I want to iterate through a vector and erase elements that meet a certain criteria. I know there is an algorithmic way of doing this, but first I would like to know how to do it with normal...
7
4320
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
2
2366
by: Angus | last post by:
Hello I have a vector<int(aRemovecoll) which is a list of the indexes to be removed from another vector. The other vecotr contains an object - I will call it SomeObject. So the other vecotr is...
0
7125
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
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
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,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
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
7379
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
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3093
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...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.