std::basic_string::replace(3)
hiii bestie π welcome to the std::basic_string::replace(3) page β thatβs the manual name, and weβre in the C++ Standard Libary (their spelling, not mine π). This is the member function that swaps out a chunk of characters in a basic_string for new ones, and yes, it has a whole overload cinematic universe. Weβre keeping every flag, range, exception, and constexpr since C++20 receipt. Periodt. β¨
β¨ NAME β¨
std::basic_string::replace β std::basic_string::replace. The name and the description are literally the same, minimal queen behavior. π
πΈ Synopsis πΈ
basic_string& replace( size_type pos,
size_type count,
const basic_string& str ); // (1) (constexpr since C++20)
basic_string& replace( const_iterator first,
const_iterator last,
const basic_string& str ); // (2) (constexpr since C++20)
basic_string& replace( size_type pos,
size_type count,
const basic_string& str,
size_type pos2,
size_type count2 ); // (until C++14)
basic_string& replace( size_type pos,
size_type count,
const basic_string& str,
size_type pos2,
size_type count2 = npos ); // (since C++14) (constexpr since C++20)
basic_string& replace( size_type pos,
size_type count,
const CharT* cstr,
size_type count2 ); // (4) (constexpr since C++20)
basic_string& replace( const_iterator first,
const_iterator last,
const CharT* cstr,
size_type count2 ); // (5) (constexpr since C++20)
basic_string& replace( size_type pos,
size_type count,
const CharT* cstr ); // (6) (constexpr since C++20)
basic_string& replace( const_iterator first,
const_iterator last,
const CharT* cstr ); // (7) (constexpr since C++20)
basic_string& replace( size_type pos,
size_type count,
size_type count2,
CharT ch ); // (8) (constexpr since C++20)
basic_string& replace( const_iterator first,
const_iterator last,
size_type count2,
CharT ch ); // (3) (9) (constexpr since C++20)
template< class InputIt >
basic_string& replace( const_iterator first,
const_iterator last,
InputIt first2,
InputIt last2 ); // (10) (constexpr since C++20)
basic_string& replace( const_iterator first,
const_iterator last,
std::initializer_list<CharT> ilist ); // (11) (since C++11) (constexpr since C++20)
template< class StringViewLike >
basic_string& replace( size_type pos,
size_type count,
const StringViewLike& t ); // (12) (since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& replace( const_iterator first,
const_iterator last,
const StringViewLike& t ); // (13) (since C++17) (constexpr since C++20)
template< class StringViewLike >
basic_string& replace( size_type pos,
size_type count,
const StringViewLike& t,
size_type pos2,
size_type count2 = npos ); // (14) (since C++17) (constexpr since C++20)
π Description π
okay so hereβs the vibe: these functions replace the characters in the range [begin() + pos, begin() + std::min(pos + count, size())) or [first, last) with the characters you give them. The exact replacement depends on which overload you pick:
- Overloads (1), (2): those characters are replaced with
str. π - Overload (3): those characters are replaced with a substring
[pos2, std::min(pos2 + count2, str.size()))ofstr. - Overloads (4), (5): those characters are replaced with the characters in the range
[cstr, cstr + count2). If[cstr, cstr + count2)is not a valid range, the behavior is undefined β not cute, do not do this. π¨ - Overloads (6), (7): those characters are replaced with the characters in the range
[cstr, cstr + Traits::length(cstr)). - Overloads (8), (9): those characters are replaced with
count2copies ofch. - Overload (10): those characters are replaced with the characters in the range
[first2, last2)as if byreplace(first, last, basic_string(first2, last2, get_allocator())). - Overload (11): those characters are replaced with the characters in
ilist. - Overloads (12), (13): implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then those characters are replaced with the characters fromsv. These overloads participate in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false. π«Ά - Overload (14): implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then those characters are replaced with the characters from the subviewsv.substr(pos2, count2). This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true andstd::is_convertible_v<const StringViewLike&, const CharT*>is false.
If [begin(), first) or [first, last) is not a valid range, the behavior is undefined. π«
π Parameters π
posβ start of the substring that is going to be replacedcountβ length of the substring that is going to be replacedfirst,lastβ range of characters that is going to be replacedstrβ string to use for replacementpos2β start of the substring to replace withcount2β number of characters to replace withcstrβ pointer to the character string to use for replacementchβ character value to use for replacementfirst2,last2β range of characters to use for replacementilistβ initializer list with the characters to use for replacementtβ object (convertible tostd::basic_string_view) with the characters to use for replacement
π¦ Type requirements π¦
InputItmust meet the requirements of LegacyInputIterator.
β Return value β
*this. the same string object, now with new characters. thank u, next. πββοΈ
π Exceptions π
- Overload (1): throws
std::out_of_rangeifpos > size(). - Overload (3): throws
std::out_of_rangeifpos > size()orpos2 > str.size(). - Overloads (4), (6), (8): throw
std::out_of_rangeifpos > size(). - Overloads (12), (14): throw
std::out_of_rangeifpos > size().
If the operation would result in size() > max_size(), it throws std::length_error.
If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee). so your string stays unbothered, fr fr. π‘οΈ
π Example π
This section is incomplete. Reason: no example.
bestie, the example literally didnβt show up π no code demo here. not me being the messenger. π
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 847β applied to C++98 β behavior as published: there was no exception safety guarantee β correct behavior: added strong exception safety guarantee. - DR
LWG 1323β applied to C++98 β behavior as published: the types offirstandlastwereiteratorβ correct behavior: changed toconst_iterator. - DR
LWG 2946β applied to C++17 β behavior as published: overloads (12,13) caused ambiguity in some cases β correct behavior: avoided by making them templates.
π See also π
replace_with_rangeβ replaces specified portion of a string with a range of characters (C++23) (public member function)regex_replaceβ replaces occurrences of a regular expression with formatted replacement text (C++11) (function template)replace,replace_ifβ replaces all values satisfying specific criteria with another value (function template)
π Category π
Category: * Todo no example β yeah, itβs a todo. The example section is still waiting for its main character moment. π
catch u later, bestie π«Ά this page came from http://cppreference.com, version/date 2024.06.10, and the manual page is std::basic_string::replace(3). mwah π
π¦ 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