473,770 Members | 6,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

off_the_end iterator to vector::erase() function

Consider
vector<stringv;

If we call,

v.erase(v.end() )

this invokes undefined behaviour.

But, if we call

v.erase(v.begin (), v.end())
or
v.erase(v.end() , v.end())

will these last two calls invoke undefined behaviour ? Why I am asking
is that v.clear() on an empty vector is valid - it does nothing.
v.clear() is equivalent to v.erase(v.begin (), v.end()) which in turn
is equal to v.erase(v.end() , v.end()) when 'v' is empty. Am I
correct ?

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #1
3 1881
My views. Guys, correct me if I am wrong.

~Moh

On Apr 30, 9:23*pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider
vector<stringv;

If we call,

v.erase(v.end() )

this invokes undefined behaviour.
Correct. Reason: According to 23.1.1 point 3 and 4 operation erase is
defined on a vector(sequence in general) v as: v.erase(iter) where
iter is a valid de-referenceable iterator. Clearly, v.end() isn't.
But, if we call

v.erase(v.begin (), v.end())
or
v.erase(v.end() , v.end())

will these last two calls invoke undefined behaviour ?
First case will not be undefined as v.begin() and v.end() defines a
valid range. [I am not dereferencing the end element] this will in
fact return me a v.end()

Why I am asking
is that v.clear() on an empty vector is valid - it does nothing.
v.clear() is equivalent to v.erase(v.begin (), v.end()) which in turn
is equal to v.erase(v.end() , v.end()) when 'v' is empty. Am I
correct ?
this should be okay. according to 23.1 we see that begin() == end() in
case the sequence is empty and we are still providing a valid range.
>
Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #2
<su************ **@yahoo.comwro te in message
news:2d******** *************** ***********@q24 g2000prf.google groups.com...
Consider
vector<stringv;

If we call,

v.erase(v.end() )

this invokes undefined behaviour.
Correct.
But, if we call

v.erase(v.begin (), v.end())
or
v.erase(v.end() , v.end())

will these last two calls invoke undefined behaviour ?
No.

The difference is that when you call v.erase with one argument, you are
asking it to erase one element. In that case, you must specify a single
element to erase, and v.end() does not specify an element.

When you call v.erase with two arguments, you are asking it to erase all of
the elements in a range. In that case, you must specify a range, and
passing v.end() twice is one legitimate way of specifying a range that does
not contain any elements.
Jun 27 '08 #3
On Apr 30, 7:53 pm, "Andrew Koenig" <a...@acm.orgwr ote:
<subramanian10. ..@yahoo.comwro te in message
news:2d******** *************** ***********@q24 g2000prf.google groups.com...
Consider
vector<stringv;
If we call,
v.erase(v.end() )
this invokes undefined behaviour.
Correct.
But, if we call
v.erase(v.begin (), v.end())
or
v.erase(v.end() , v.end())
will these last two calls invoke undefined behaviour ?
No.
The difference is that when you call v.erase with one
argument, you are asking it to erase one element.
Couldn't one say that logically, "v.erase( i )" is the
equivalent of "v.erase( i, i + 1 )" (or whatever is necessary to
obtain an iterator one further in the container). In which
case, it's rather obvious why "v.erase( v.end() )" is illegal.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4

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

Similar topics

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); } }
10
4585
by: Alex Vinokur | last post by:
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
9
5212
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10); vector<double>::iterator i=x.begin()+2, j=x.begin()+6; x.erase(i,j); // i<j, ok, erases 4 elements. x.erase(j,i); // j>i, no error, just inserts 4 elements.
6
3405
by: happyvalley | last post by:
Hi, I want to remove some elements from a vector, the following code doesn't work, seems it doesn't allow me to remove an element when iterating the vector. (make sense), just wonder, how to do this? thank vector<intIntVec; vector<int>::iterator intIterator; for(int i=0; i<10;i++) IntVec.push_back(i);
3
2366
by: ma740988 | last post by:
For discussion purposes, assume the vector ivec contains 67108864 (67 million elements) elements. Lets futher assume nstart and end equal 1008000 and 11088000 respectively. The question. What's the _fastest_ way to erase element numbers less than 1008000 and element numbers greater than 11088000. Current approach. typedef std::vector < int INT_VEC ;
5
2055
by: Anil | last post by:
I am facing problem while erasing an elemet from stl vector when its size is 2. It works fine when SIZE 2. Can anybody help me in this?? Following is the sample code which i tried. #include <iostream> #include <vector> using namespace std; #define SIZE 2
5
1482
by: subramanian100in | last post by:
Suppose I have vector<intcontainer; Suppose I store some values into "container". Suppose that "left" and "right" are valid iterators into "container" and that "right" is NOT container.end() and "right" comes after "left".
13
7493
by: thomas | last post by:
suppose I will delete an element pointed to by "iter". like this: vector<ints; for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){ if(*iter==3) s.erase(iter); //A } in line A, if element by "iter" is erased, will "iter" point to the
1
1830
mickey0
by: mickey0 | last post by:
Hello I have a vector and an index 'i'; I need something like: for (int i = vec.size() - 1 ;i >= 0; i--) { if (something(i) ) vec.removeAt(i); } using c++ standard library; is it possibile from an int gets the iterator?
0
9592
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
9425
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
10058
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
10004
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
9870
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
7416
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
5313
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
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2817
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.