std::basic_...assign_range(3)

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

πŸ’– NAME πŸ’–

Manual page from C++ Standard Libary for std::basic_...assign_range(3): std::basic_string::assign_range - std::basic_string::assign_range. Yes bestie, the name is the name, and we are literally here for it. πŸ’…


✨ Synopsis ✨

This is giving C++23 glow-up, no cap:

template< container-compatible-range<CharT> R >       (since C++23)
constexpr std::basic_string& assign_range( R&& rg );

It replaces the contents of the string with the values in the range rg. Iconic behavior, honestly.

Equivalent to:

return assign(
    std::basic_string(
        std::from_range,
        std::forward<R>(rg),
        get_allocator())
);

🌸 Parameters 🌸

  • rg β€” a container compatible range. That's the whole vibe: a range that is container compatible, fr fr.

🫢 Return value 🫢

*this β€” that's it, that's the return value. Main character energy returns itself. πŸ‘‘


πŸ”₯ Complexity πŸ”₯

Linear in size of rg. Simple, clean, ate. ✨


😭 Exceptions 😭

If the operation would result in size() > max_size(), it throws std::length_error.

If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). So it's got that strong-safety armor on, periodt. πŸ’€


🧚 Notes 🧚

Feature-test macro vibe check:

  • __cpp_lib_containers_ranges β€” value: 202202L, Std: (C++23), Feature: member functions that accept container compatible range.

πŸŽ€ Example πŸŽ€

// Run this code

#include <cassert>
#include <string>

int main()
{
    const auto source = {'s', 'o', 'u', 'r', 'c', 'e'};
    std::string destination{"destination"};

#ifdef __cpp_lib_containers_ranges
    destination.assign_range(source);
#else
    destination.assign(source.begin(), source.end());
#endif

    assert(destination == "source");
}

🌈 See also 🌈

  • assign β€” assign characters to a string (public member function).
  • operator= β€” assigns values to the string (public member function).
  • constructor β€” constructs a basic_string (public member function).

πŸ’‹ mwah, that's the page! Source/version: http://cppreference.com, date: 2024.06.10, and the page is std::basic_...assign_range(3). Byeee 🫢

πŸ“¦ where this page comes from