close
Namespaces
Variants

Template talk:cpp/container/insert

From cppreference.com

Is this true for all containers?

quote:
first, last - the pair of iterators defining the range of elements to insert, cannot be iterators into container for which insert is called
/quote
I mean the case std::list when pos is out of range. Dmi3 (talk) 04:26, 18 February 2025 (PST)

I agree it seems like an unnecessary limitation, but it is true. It's a generic SequenceContainer requirement [1] that std::list doesn't seem to override [2]. Ybab321 (talk) 19:27:33, 18 February 2025 (PST)
We only may speculate that on any reasonable implementation a copying (not moving) of a subrange works for:
  • std::list:
    • if the source subrange does not contain the insertion point. (Actually, it is possible to insert the destination sub-list inside the source sub-list: for that, the resulted sub-list should be fully constructed before insertion. --Space Mission (talk) 14:21, 22 February 2025 (PST))
  • std::vector / std::inplace_vector:
  1. if the insertion point is located strictly after the source subrange, and
  2. The capacity has enough space: v.capacity() - v.size() >= distance(first, last).
--Space Mission (talk) 11:32, 22 February 2025 (PST)