473,593 Members | 2,841 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 4563

"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
2257
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
3368
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
1993
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
12589
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
2889
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
3342
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
18572
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
4340
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
2374
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
7936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7997
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
6646
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
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
5402
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();...
1
2383
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
0
1203
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.