insert() operation on vector, deque, list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    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
  • Kai-Uwe Bux

    #2
    Re: insert() operation on vector, deque, list

    subramanian100i n@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

    Comment

    Working...