std::deque::insert(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::deque::insert(3) πŸ’…

okay so welcome to the std::deque::insert(3) page from the C++ Standard Libary manual, bestie. Buckle up, because we're inserting elements into a container and the vibes are iconic but the rules are very real. ✨


πŸŽ€ NAME πŸŽ€

std::deque::insert β€” std::deque::insert (yes, it's literally its own name, main character energy πŸ‘‘).

πŸ¦‹ Synopsis πŸ¦‹

iterator insert( const_iterator pos, const T& value );                  // (1)
iterator insert( const_iterator pos, T&& value );                      // (2) (since C++11)
iterator insert( const_iterator pos, size_type count, const T& value ); // (3)
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );     // (4)
iterator insert( const_iterator pos, std::initializer_list<T> ilist );  // (5) (since C++11)

The core vibe: it inserts elements at the specified location in the container. πŸ’–

  • 1, 2) Inserts value before pos.
  • 3) Inserts count copies of the value before pos.
  • 4) Inserts elements from range [first, last) before pos.

Some important lore for overload (4):

  • Until C++11: this overload has the same effect as overload (3) if InputIt is an integral type.
  • Since C++11: this overload participates in overload resolution only if InputIt qualifies as LegacyInputIterator, to avoid ambiguity with overload (3).
🚨 If first and last are iterators into *this, the behavior is undefined. No cap, do not do that.
  • 5) Inserts elements from initializer list ilist before pos.

And the iterator tea β˜•: all iterators (including the end() iterator) are invalidated. References are invalidated too, unless pos == begin() or pos == end(), in which case they are not invalidated. iykyk πŸ’…

🫢 Parameters 🫢

  • pos β€” iterator before which the content will be inserted (pos may be the end() iterator).
  • value β€” element value to insert.
  • count β€” number of elements to insert.
  • first, last β€” the range of elements to insert, cannot be iterators into container for which insert is called.
  • ilist β€” initializer list to insert the values from.

🌈 Type requirements 🌈

  • T must meet the requirements of CopyAssignable and CopyInsertable in order to use overload (1).
  • T must meet the requirements of MoveAssignable and MoveInsertable in order to use overload (2).
  • T must meet the requirements of CopyAssignable and CopyInsertable in order to use overload (3).
  • T must meet the requirements of EmplaceConstructible in order to use overloads (4,5).
  • T must meet the requirements of Swappable, MoveAssignable, MoveConstructible and MoveInsertable in order to use overloads (4,5). (since C++17)

πŸ“ Return value πŸ“

  • 1, 2) Iterator pointing to the inserted value.
  • 3) Iterator pointing to the first element inserted, or pos if count == 0.
  • 4) Iterator pointing to the first element inserted, or pos if first == last.
  • 5) Iterator pointing to the first element inserted, or pos if ilist is empty.

🧚 Complexity 🧚

  • 1, 2) Constant plus linear in the lesser of the distances between pos and either of the ends of the container.
  • 3) Linear in count plus linear in the lesser of the distances between pos and either of the ends of the container.
  • 4) Linear in std::distance(first, last) plus linear in the lesser of the distances between pos and either of the ends of the container.
  • 5) Linear in ilist.size() plus linear in the lesser of the distances between pos and either of the ends of the container.

πŸ’€ Exceptions πŸ’€

If an exception is thrown other than by:

  • the copy constructor of T,
  • the move constructor of T, (since C++11)
  • the copy assignment operator of T,
  • the move assignment operator of T, (since C++11)

then this function has no effect (strong exception guarantee). πŸ›‘οΈ

If an exception is thrown when inserting a single element at either end, this function has no effect (strong exception guarantee). (since C++11)

Otherwise, if an exception is thrown by the move constructor of a non-CopyInsertable T, the effects are unspecified. 😭

πŸŽ€ Example πŸŽ€

The original page says // Run this code β€” so yeah, run it, bestie. πŸ’»βœ¨

#include <iostream>
#include <iterator>
#include <deque>

void print(int id, const std::deque<int>& container)
{
    std::cout << id << ". ";
    for (const int x : container)
        std::cout << x << ' ';
    std::cout << '\n';
}

int main ()
{
    std::deque<int> c1(3, 100);
    print(1, c1);

    auto it = c1.begin();
    it = c1.insert(it, 200);
    print(2, c1);

    c1.insert(it, 2, 300);
    print(3, c1);

    // reset `it` to the begin:
    it = c1.begin();

    std::deque<int> c2(2, 400);
    c1.insert(std::next(it, 2), c2.begin(), c2.end());
    print(4, c1);

    int arr[] = {501, 502, 503};
    c1.insert(c1.begin(), arr, arr + std::size(arr));
    print(5, c1);

    c1.insert(c1.end(), {601, 602, 603});
    print(6, c1);
}

πŸ“ Output πŸ“

1. 100 100 100
2. 200 100 100 100
3. 300 300 200 100 100 100
4. 300 300 400 400 200 100 100 100
5. 501 502 503 300 300 400 400 200 100 100 100
6. 501 502 503 300 300 400 400 200 100 100 100 601 602 603

πŸ’… Defect reports πŸ’…

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History lesson, but make it fashion πŸ’œ

  • LWG 149, applied to C++98: behavior as published β€” overloads (3) and (4) returned nothing; correct behavior β€” returns an iterator.
  • LWG 247, applied to C++98: behavior as published β€” the complexity was only specified for inserting a single element; correct behavior β€” also specified for inserting multiple elements.

πŸ‘€ See also πŸ‘€

  • emplace (C++11) β€” constructs element in-place (public member function)
  • push_front β€” inserts an element to the beginning (public member function)
  • push_back β€” adds an element to the end (public member function)
  • inserter β€” creates a std::insert_iterator of type inferred from the argument (function template)

Source/version/date sign-off: this came from http://cppreference.com, version/date 2024.06.10, and the page is std::deque::insert(3). That's the whole manual page, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from