std::vector::append_range(3)

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

πŸ¦‹ std::vector::append_range(3) πŸ¦‹

okay bestie, welcome to the C++ Standard Libary page for std::vector::append_range β€” yes, the manual name says β€œLibary,” and we respect the canon πŸ’…. This is your C++23 era, and it's giving β€œappend a whole range, not just one lil element” energy ✨.

🌸 NAME 🌸

  • std::vector::append_range β€” std::vector::append_range. Like, the name is the name, no cap 😭.

✨ Synopsis ✨

template< container-compatible-range<T> R >  (since C++23)
constexpr void append_range( R&& rg );

It inserts copies of the elements from the range rg before end(), in non-reversing order. Periodt.

If, after the operation, the new size() is greater than the old capacity(), a reallocation takes place. When that happens, all iterators (including the end() iterator) and all references to the elements are invalidated. Otherwise, only the end() iterator is invalidated. Each iterator in rg is dereferenced exactly once β€” once, bestie, not extra πŸ’….

🌈 Parameters 🌈

  • rg β€” a container compatible range, that is, an input_range whose elements are convertible to T.

🧚 Type requirements 🧚

  • T must be EmplaceConstructible into vector from *ranges::begin(rg). Also, T must be MoveInsertable into vector. Otherwise, the behavior is undefined.
🚨 undefined behavior is not the vibe. not slay. do not collect $200.

πŸŽ€ Return value πŸŽ€

(none) β€” literally nothing returned, it's giving void energy 🫢.

πŸ”₯ Complexity πŸ”₯

If reallocation happens, linear in the number of elements of the resulting vector; otherwise, linear in the number of elements inserted plus the distance to the end().

😭 Exceptions 😭

If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of T, or by any InputIterator operation, there are no effects.

If an exception is thrown while inserting a single element at the end and T is CopyInsertable or std::is_nothrow_move_constructible_v<T> is true, there are no effects.

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

πŸ† Notes πŸ†

Feature-test macro vibe check:

  • __cpp_lib_containers_ranges β€” value 202202L, std (C++23), feature: Ranges-aware construction and insertion.

🌟 Example 🌟

Run this code, bestie:

#include <algorithm>
#include <cassert>
#include <vector>
#include <list>

int main()
{
    auto head = std::vector{1, 2, 3, 4};
    const auto tail = std::list{-5, -6, -7};
#ifdef __cpp_lib_containers_ranges
    head.append_range(tail);
#else
    head.insert(head.end(), tail.cbegin(), tail.cend());
#endif
    assert(std::ranges::equal(head, std::vector{1, 2, 3, 4, -5, -6,
    -7}));
}

πŸ‘€ See also πŸ‘€

  • insert_range β€” inserts a range of elements (C++23) (public member function).
  • push_back β€” adds an element to the end (public member function).
  • emplace_back β€” constructs an element in-place at the end (C++11) (public member function).

okay that’s the whole page, bestie 🫢 source: http://cppreference.com, version/date: 2024.06.10, for std::vector::append_range(3) β€” mwah, slay, bye πŸ’–

πŸ“¦ where this page comes from