473,769 Members | 5,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert() operation on vector, deque, list

Suppose I have

vector<intvi;
deque<intdi;
list<intli;

Suppose all of these containers have some elements.

Suppose 'v_iter' is an iterator pointing to some element in 'vi'.
Suppose 'v_beg' and 'v_end' are valid iterators pointing to some
elements in 'vi' itself; but 'v_iter' does not fall in the range
[v_begin, v_end).

Then the operation vi.insert(v_ite r, v_beg, v_end) is INVALID because
v_beg and v_end may be invalidated after the insertion of the first
element in the range [v_begin, v_end).

Same reason applies to deque for the same operation di.insert(d_ite r,
d_beg, d_end);

Is my above understanding correct ?

However, suppose 'l_iter' is an iterator pointing to some element in
'li'. Suppose 'l_beg' and 'l_end' are valid iterators pointing to some
elements in 'li' itself; but 'l_iter' does not fall in the range
[l_begin, l_end).

Then, the operation li.insert(l_ite r, l_beg, l_end) is valid because
for a list, by the way of insertion, the iterators l_beg and l_end are
not invalidated for list.
Is this reason correct?

Kindly clarify.

Thanks
V.Subramanian
Mar 3 '08 #1
1 2538
su************* *@yahoo.com, India wrote:
Suppose I have

vector<intvi;
deque<intdi;
list<intli;

Suppose all of these containers have some elements.

Suppose 'v_iter' is an iterator pointing to some element in 'vi'.
Suppose 'v_beg' and 'v_end' are valid iterators pointing to some
elements in 'vi' itself; but 'v_iter' does not fall in the range
[v_begin, v_end).

Then the operation vi.insert(v_ite r, v_beg, v_end) is INVALID because
v_beg and v_end may be invalidated after the insertion of the first
element in the range [v_begin, v_end).

Same reason applies to deque for the same operation di.insert(d_ite r,
d_beg, d_end);

Is my above understanding correct ?
No. The insertion is invalid because the standard states [Table 67] as a
precondition that the range arguments to insert must not be iterators into
the sequence into which you insert. Whether iterators are invalidated or
not is immaterial.

However, suppose 'l_iter' is an iterator pointing to some element in
'li'. Suppose 'l_beg' and 'l_end' are valid iterators pointing to some
elements in 'li' itself; but 'l_iter' does not fall in the range
[l_begin, l_end).

Then, the operation li.insert(l_ite r, l_beg, l_end) is valid because
for a list, by the way of insertion, the iterators l_beg and l_end are
not invalidated for list.
Is this reason correct?
I don't think so. As far as I know the precondition in Table 67 applies to
lists as well. I am not aware of additional language in the standard that
makes an exception for lists. As far as I can tell, you have undefined
behavior either way.
Best

Kai-Uwe Bux
Mar 3 '08 #2

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

Similar topics

19
11315
by: John Keeling | last post by:
Dear all, I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows: time_reverse 0.889999389648 time_insert 15.7750005722 Over multiple runs ... the time taken to insert at the head of a list, vs. the time taken to append to a list and then reverse it is typically 16 or 17 times longer. I would have expected the insert...
12
10947
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to the end - obviously its O(n). However, keeping track of inserts and deletes isn't that tough, so its conceivable (to me!) that size() could be O(1). For my application, the lists can be quite long (millions of elements), so the answer greatly...
1
1907
by: Mattias B | last post by:
Hello! I have a question about how stl's insert works on containers and on vector in particular. The question is: is it OK to insert a value before end() with something like: vector<int> v; v.insert(v.end(), 1);
29
3975
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My case: class A {
11
2906
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...
34
4176
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
4
1882
by: Tony | last post by:
Hello, For my application, I have a vector containing data that I need, vector<NODEnode_data; and I have a pointer of vectors which are sorted by some criterion as vector<NODE*np. I have planned the data management as: Whenever I want to add a new node, I push_back onto the end of node_data and create a corresponding reference in np. However, I find that only the last-added pointer in np correctly points to the right
32
4033
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
0
1174
by: subramanian100in | last post by:
Suppose I have a vector<Tor deque<Tfor some type T. Suppose vector<T>::iterator iter is an iterator. Then consider the operation vector<T>::iterator nit = container.insert(iter, T()); After the insertion, iter will be invalidated for both vector<T>, deque<T>. However for list<T>, the same operation under g++ 3.4.3, does not seem
0
9583
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
9423
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
10039
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
9990
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
8869
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...
0
6668
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
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
2814
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.