insert() operation on vector, deque, list

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Suppose I have

vector<int> vi;
deque<int> di;
list<int> li;

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
 
K

Kai-Uwe Bux

Suppose I have

vector<int> vi;
deque<int> di;
list<int> li;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top