473,785 Members | 2,863 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>::it erator,
rbegin() returns vector<int>::re verse_iterator.

So, a compiler doesn't accept v.erase(v.rbegi n()).
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 4585

"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>::it erator,
rbegin() returnsvectorin treverse_iterat or.

So, a compiler doesn't accept v.erase(v.rbegi n()).
Do we have to writ
ev.eraseOrthere existssomething else


reverse_iterato rs 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>::it erator,
rbegin() returnsvectorin treverse_iterat or.

So, a compiler doesn't accept v.erase(v.rbegi n()).
Do we have to writ
ev.eraseOrthere existssomething else


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

john


I have got

assertion "assert ((*v.rbegin()) == (*v.rbegin().ba se()))" 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>::it erator,
rbegin() returnsvectorin treverse_iterat or.

So, a compiler doesn't accept v.erase(v.rbegi n()).
Do we have to writ
ev.eraseOrthe reexistssomethi ngelse

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

john

From TC++PL 3:
"A reverse_iterato r 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_iterato r’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_iterato r refers to. This implies that * returns the
value *(current-1) and that ++ is implemented using -- on current".

In other words the base() of reverse_iterato r returns current, which is
one after the element that reverse_iterato r points, under the regular
iterator point of view.
So if you want to point to the same element with reverse_iterato r,
provided that reverse_iterato r does not point at "one past the end"
element of its view, you can do for example:
vector<int>::re verse_iterator rp = whatever;
// Not to be used for a reverse_iterato r pointing to "one past the end"
// element under its point of view.
vector<int>::it erator 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().ba se()))" 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

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

Similar topics

0
2263
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 != v.rend(); i++) { something = (*i); }
9
3377
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; if (ccu->GetCutoff() >= high_cutoff) { good_list.erase(it); } }
3
1997
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 enter note number to clear: "; cin >> clear_note; clear_note = clear_note - 1; vec.erase(clear_note);
7
12631
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 added at the front (index 0) of the vector. If the vector contains a certain amount of elements, the element at the back is removed when a new one is added. If one tries to add a string already stored in the vector, that string is supposed to...
11
2907
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 circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
17
3361
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
14
18588
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 iteration. I'm am trying to do something like: vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4);
7
4353
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
2382
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 a vector<SomeObject>. I have created a function like this so far: UINT uNumElements = aRemoveColl.GetSize();
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.