473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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_iter, 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_iter,
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_iter, 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 2520
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_iter, 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_iter,
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_iter, 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
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:...
12
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...
1
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>...
29
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...
11
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...
34
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...
4
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...
32
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...

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.