std::basic_...e_with_range(3)
π std::basic_string::replace_with_range π
okay bestie, this one comes straight from the C++ Standard Libary manual β and yes, that's how it's spelled in the header, I'm not fixing it, I'm just delivering the facts with glitter. β¨ The header title is out here looking like std::basic_...e_with_range(3), so consider this your very cute near-the-top manual-name moment. π«Ά
πΈ NAME πΈ
std::basic_string::replace_with_range β and the NAME section literally says it twice: std::basic_string::replace_with_range. We love a name so nice they had to say it twice. π
β¨ Synopsis β¨
template< container-compatible-range<CharT> R >
constexpr std::basic_string& replace_with_range( const_iterator
first,
(since C++23)
const_iterator last,
R&& rg );
So this one replaces the characters in the range [first, last) with the characters from the range rg. It is equivalent to this exact code right here:
return replace(first,
last,
std::basic_string(
std::from_range,
std::forward<R>(rg),
get_allocator())
);
π Parameters π
first,lastβ range of characters that is going to be replaced.rgβ a container compatible range.
π¦ Return value π¦
*this
πΈ Complexity πΈ
Linear in size of rg.
π 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 β that's the strong exception safety guarantee, bestie. No messy half-done vibes. π«Ά
π Notes π
- Feature-test macro:
__cpp_lib_containers_rangesβ value202202L, std(C++23), feature: member functions that accept container compatible range.
π Example π
// Run this code
#include <algorithm>
#include <cassert>
#include <forward_list>
#include <iterator>
#include <string>
int main()
{
using namespace std::literals;
auto s{"Today is today!"s};
constexpr auto today{"today"sv};
constexpr auto tomorrow{"tomorrow's yesterday"sv};
std::forward_list<char> rg;
std::ranges::reverse_copy(tomorrow, std::front_inserter(rg));
const auto pos{s.rfind(today)};
assert(pos != s.npos);
const auto first{std::next(s.begin(), pos)};
const auto last{std::next(first, today.length())};
#ifdef __cpp_lib_containers_ranges
s.replace_range(first, last, rg);
#else
s.replace(first, last, rg.cbegin(), rg.cend());
#endif
assert("Today is tomorrow's yesterday!" == s);
}
The code block above is exactly as the page gives it β including the s.replace_range(first, last, rg); line. I am not fixing a single character, bestie. π
β See also β
replaceβ replaces specified portion of a string (public member function).
okay that's the whole page π β source/version is http://cppreference.com and the date on it is 2024.06.10, for the std::basic_...e_with_range(3) page. periodt. π«Άβ¨
π¦ 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