std::forwar...epend_range(3)

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

🌸 std::forward_list::prepend_range β€” C++ Standard Library, section 3 🌸

πŸ’– NAME πŸ’–

std::forward_list::prepend_range β€” yes bestie, that's the whole name: std::forward_list::prepend_range. We're so back.


✨ SYNOPSIS ✨

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

Okay so picture this: prepend_range inserts, in non-reversing order, copies of the elements in rg before begin(). Every iterator in the range rg gets dereferenced exactly once β€” not twice, not "oops I did it again" twice. And the best part? No iterators or references are invalidated. Iconic behavior, honestly.


πŸ¦‹ PARAMETERS πŸ¦‹

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

🧚 TYPE REQUIREMENTS 🧚

  • T must be EmplaceConstructible into forward_list from *ranges::begin(rg). Otherwise, the behavior is undefined. And bestie, "undefined behavior" is not a cute vibe β€” it's a real warning. πŸ’€

πŸŽ€ RETURN VALUE πŸŽ€

(none) β€” literally nothing comes back. It's giving "do the thing and leave", and honestly? Respect.


πŸ’… COMPLEXITY πŸ’…

Linear in size of rg. No cap.


πŸ”₯ NOTES πŸ”₯

Feature-test macro check, because C++23 said "new feature, who dis?":

  • 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 <forward_list>
        #include <vector>

        int main()
        {
            auto container = std::forward_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_after(container.before_begin(), rg.cbegin(),
       rg.cend());
        #endif
            assert(std::ranges::equal(container, std::forward_list{-3, -2, -1,
       0, 1, 2, 3}));
        }

πŸ‘‘ SEE ALSO πŸ‘‘

  • insert_range β€” inserts a range of elements (C++23) (public member function).
  • insert_range_after β€” inserts a range of elements after an element (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 (public member function).

πŸ’– that's the whole vibe β€” sourced from http://cppreference.com, dated 2024.06.10. bye bestie, go slay some ranges! πŸ’…βœ¨

πŸ“¦ where this page comes from