std::list::prepend_range(3)

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

🌸 NAME 🌸

okay bestie, you're on the std::list::prepend_range(3) page from the C++ Standard Libary manual πŸ’… β€” yes, that spelling is in the header, we keep it.

std::list::prepend_range - std::list::prepend_range (the NAME line is literally twins, iconic).

πŸ¦‹ Synopsis πŸ¦‹

so the signature is giving:

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

What it does: inserts, in non-reversing order, copies of elements in rg before begin(). Each iterator in the range rg is dereferenced exactly once.

No iterators or references are invalidated. periodt.

πŸ’– Parameters πŸ’–

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

✨ Type requirements ✨

  • T must be EmplaceConstructible into list from *ranges::begin(rg).
⚠️ Otherwise, the behavior is undefined β€” not a vibe, bestie. Do not test fate.

πŸŽ€ Return value πŸŽ€

(none) β€” literally nothing comes back. it's giving void, and that's fine πŸ’–

πŸ”₯ Complexity πŸ”₯

Linear in size of rg. so yeah, it scales with the range, no surprises.

πŸ’… Notes πŸ’…

Feature-test macro tea πŸ’…:

  • Feature-test macro: __cpp_lib_containers_ranges
  • Value: 202202L
  • Std: (C++23)
  • Feature: Ranges-aware construction and insertion

🌈 Example 🌈

// Run this code

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

int main()
{
    auto container = std::list{0, 1, 2, 3};
    const auto rg = std::vector{-3, -2, -1};

#if __cpp_lib_containers_ranges
    container.prepend_range(rg);
#else
    container.insert(container.begin(), rg.cbegin(), rg.cend());
#endif
    assert(std::ranges::equal(container, std::list{-3, -2, -1, 0, 1, 2, 3}));
}

πŸ‘‘ See also πŸ‘‘

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

later, bestie 🫢 β€” source/version/date: http://cppreference.com Β· 2024.06.10 Β· std::list::prepend_range(3).

πŸ“¦ where this page comes from