std::basic_...insert_range(3)
π NAME π
okay bestie, welcome to the std::basic_...insert_range(3) page from the C++ Standard Libary (yes, that spelling, the source said it and Iβm not fixing it π
). The name line is literally:
std::basic_string::insert_range - std::basic_string::insert_range
short, iconic, no notes. π
πΈ Synopsis πΈ
template< container-compatible-range<CharT> R >
(since C++23)
constexpr iterator insert_range( const_iterator pos, R&& rg );
This one inserts characters from the range rg before the element (if any) pointed by pos. Itβs giving βsneak the range in right hereβ energy.
Equivalent to:
return insert(pos - begin(),
std::basic_string(
std::from_range,
std::forward<R>(rg),
get_allocator())
);
π¨ Ifposis not a valid iterator on*this, the behavior is undefined. Not a vibe check you want to fail, bestie.
π Parameters π
posβ iterator before which the characters will be inserted.rgβ a container compatible range.
π¦ Return value π¦
An iterator which refers to the first inserted character, or pos if no characters were inserted because rg was empty. So you either get the first new characterβs iterator, or the original pos when rg was empty. Thatβs the whole tea. π«
β Complexity β
Linear in size of rg. No cap, it scales with rg.
π Exceptions π
- If
std::allocator_traits<Allocator>::allocatethrows an exception, it is rethrown. - If the operation would result in
size() > max_size(), throwsstd::length_error. - If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
π Notes π
The feature-test macro table is giving:
- Feature-test macro:
__cpp_lib_containers_ranges - Value:
202202L - Std:
(C++23) - Feature: member functions that accept container compatible range
Thatβs the whole table, bestie β memorize the macro, iykyk. π
π Example π
// Run this code
#include <cassert>
#include <iterator>
#include <string>
int main()
{
const auto source = {'l', 'i', 'b', '_'};
std::string target{"__cpp_containers_ranges"};
// ^insertion will occur
// before this position
const auto pos = target.find("container");
assert(pos != target.npos);
auto iter = std::next(target.begin(), pos);
#ifdef __cpp_lib_containers_ranges
target.insert_range(iter, source);
#else
target.insert(iter, source.begin(), source.end());
#endif
assert(target == "__cpp_lib_containers_ranges");
// ^^^^
}
π«Ά See also π«Ά
insertβ inserts characters (public member function)
okay bye bestie π this was the std::basic_...insert_range(3) page, sourced from http://cppreference.com, dated 2024.06.10. mwah, stay strong, and may your iterators always be valid. π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms