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
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