Files
2025-10-25 03:02:53 +03:00

318 lines
9.2 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[string.replace]
# 27 Strings library [[strings]](./#strings)
## 27.4 String classes [[string.classes]](string.classes#string.replace)
### 27.4.3 Class template basic_string [[basic.string]](basic.string#string.replace)
#### 27.4.3.7 Modifiers [[string.modifiers]](string.modifiers#string.replace)
#### 27.4.3.7.6 basic_string::replace [string.replace]
[🔗](#lib:replace,basic_string)
`constexpr basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3965)
*Effects*: Equivalent to: return replace(pos1, n1, str.data(), str.size());
[🔗](#lib:replace,basic_string_)
`constexpr basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
size_type pos2, size_type n2 = npos);
`
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3977)
*Effects*: Equivalent to:return replace(pos1, n1, basic_string_view<charT, traits>(str).substr(pos2, n2));
[🔗](#lib:replace,basic_string__)
`template<class T>
constexpr basic_string& replace(size_type pos1, size_type n1, const T& t);
`
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L3992)
*Constraints*:
- [(3.1)](#3.1)
is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and
- [(3.2)](#3.2)
is_convertible_v<const T&, const charT*> isfalse[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4003)
*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return replace(pos1, n1, sv.data(), sv.size());
[🔗](#lib:replace,basic_string___)
`template<class T>
constexpr basic_string& replace(size_type pos1, size_type n1, const T& t,
size_type pos2, size_type n2 = npos);
`
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4020)
*Constraints*:
- [(5.1)](#5.1)
is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and
- [(5.2)](#5.2)
is_convertible_v<const T&, const charT*> isfalse[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4031)
*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return replace(pos1, n1, sv.substr(pos2, n2));
[🔗](#lib:replace,basic_string____)
`constexpr basic_string& replace(size_type pos1, size_type n1, const charT* s, size_type n2);
`
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4046)
*Preconditions*: [s, s + n2) is a valid range[.](#7.sentence-1)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4050)
*Effects*: Determines the effective length xlen of the string to be
removed as the smaller of n1 and size() - pos1[.](#8.sentence-1)
Ifsize() - xlen >= max_size() - n2 throws length_error[.](#8.sentence-2)
Otherwise,
the function replaces the characters in the range
[begin() + pos1, begin() + pos1 + xlen)
with a copy of the range [s, s + n2)[.](#8.sentence-3)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4059)
*Returns*: *this[.](#9.sentence-1)
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4063)
*Throws*:
- [(10.1)](#10.1)
out_of_range if pos1 > size(),
- [(10.2)](#10.2)
length_error if the length of the resulting string
would exceed max_size(), or
- [(10.3)](#10.3)
any exceptions thrown by allocator_traits<Allocator>::allocate[.](#10.sentence-1)
[🔗](#lib:replace,basic_string_____)
`constexpr basic_string& replace(size_type pos, size_type n, const charT* s);
`
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4079)
*Effects*: Equivalent to: return replace(pos, n, s, traits::length(s));
[🔗](#lib:replace,basic_string______)
`constexpr basic_string& replace(size_type pos1, size_type n1, size_type n2, charT c);
`
[12](#12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4090)
*Effects*: Determines the effective length xlen of the string to be
removed as the smaller of n1 and size() - pos1[.](#12.sentence-1)
Ifsize() - xlen >= max_size() - n2 throws length_error[.](#12.sentence-2)
Otherwise,
the function replaces the characters in the range
[begin() + pos1, begin() + pos1 + xlen)
with n2 copies of c[.](#12.sentence-3)
[13](#13)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4099)
*Returns*: *this[.](#13.sentence-1)
[14](#14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4103)
*Throws*:
- [(14.1)](#14.1)
out_of_range if pos1 > size(),
- [(14.2)](#14.2)
length_error if the length of the resulting string
would exceed max_size(), or
- [(14.3)](#14.3)
any exceptions thrown by allocator_traits<Allocator>::allocate.
[🔗](#lib:replace,basic_string_______)
`constexpr basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
`
[15](#15)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4119)
*Effects*: Equivalent to: return replace(i1, i2, basic_string_view<charT, traits>(str));
[🔗](#lib:replace,basic_string________)
`template<class T>
constexpr basic_string& replace(const_iterator i1, const_iterator i2, const T& t);
`
[16](#16)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4131)
*Constraints*:
- [(16.1)](#16.1)
is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and
- [(16.2)](#16.2)
is_convertible_v<const T&, const charT*> isfalse[.](#16.sentence-1)
[17](#17)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4142)
*Preconditions*: [begin(), i1) and [i1, i2) are valid ranges[.](#17.sentence-1)
[18](#18)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4146)
*Effects*: Equivalent to:basic_string_view<charT, traits> sv = t;return replace(i1 - begin(), i2 - i1, sv.data(), sv.size());
[🔗](#lib:replace,basic_string_________)
`constexpr basic_string& replace(const_iterator i1, const_iterator i2, const charT* s, size_type n);
`
[19](#19)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4161)
*Effects*: Equivalent to: return replace(i1, i2, basic_string_view<charT, traits>(s, n));
[🔗](#lib:replace,basic_string__________)
`constexpr basic_string& replace(const_iterator i1, const_iterator i2, const charT* s);
`
[20](#20)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4172)
*Effects*: Equivalent to: return replace(i1, i2, basic_string_view<charT, traits>(s));
[🔗](#lib:replace,basic_string___________)
`constexpr basic_string& replace(const_iterator i1, const_iterator i2, size_type n, charT c);
`
[21](#21)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4183)
*Preconditions*: [begin(), i1) and [i1, i2) are valid ranges[.](#21.sentence-1)
[22](#22)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4187)
*Effects*: Equivalent to: return replace(i1 - begin(), i2 - i1, n, c);
[🔗](#lib:replace,basic_string____________)
`template<class InputIterator>
constexpr basic_string& replace(const_iterator i1, const_iterator i2,
InputIterator j1, InputIterator j2);
`
[23](#23)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4200)
*Constraints*: InputIterator is a type that qualifies as an input
iterator ([[container.reqmts]](container.reqmts "23.2.2.2Container requirements"))[.](#23.sentence-1)
[24](#24)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4205)
*Effects*: Equivalent to: return replace(i1, i2, basic_string(j1, j2, get_allocator()));
[🔗](#lib:replace_with_range,basic_string)
`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<charT> R>
constexpr basic_string& replace_with_range(const_iterator i1, const_iterator i2, R&& rg);
`
[25](#25)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4217)
*Effects*: Equivalent to:return replace(i1, i2, basic_string(from_range, std::forward<R>(rg), get_allocator()));
[🔗](#lib:replace,basic_string_____________)
`constexpr basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<charT> il);
`
[26](#26)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L4231)
*Effects*: Equivalent to: return replace(i1, i2, il.begin(), il.size());