C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string 的推导指引
- std::basic_string_view
- 空终止字节字符串
- 空终止多字节字符串
- 空终止宽字符串
- std::char_traits
- 注释
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::basic_string<CharT,Traits,Allocator>::replace
std::basic_string<CharT,Traits,Allocator>::replace
const basic_string& str,
const basic_string& str,
(C++20 前)
const basic_string& str,
basic_string& replace( const_iterator first, const_iterator last,
constexpr basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
const CharT* cstr, size_type count2 );
const CharT* cstr, size_type count2 );
const CharT* cstr, size_type count2 );
const CharT* cstr );
const CharT* cstr );
const CharT* cstr );
const CharT* cstr );
size_type count2, CharT ch );
size_type count2, CharT ch );
size_type count2, CharT ch );
size_type count2, CharT ch );
std::initializer_list<CharT> ilist );
(C++20 前)
std::initializer_list<CharT> ilist );
basic_string& replace( size_type pos, size_type count,
(C++20 前)
constexpr basic_string& replace( size_type pos, size_type count,
basic_string& replace( const_iterator first, const_iterator last,
(C++20 前)
constexpr basic_string& replace( const_iterator first, const_iterator last,
basic_string& replace( size_type pos, size_type count, const T& t,
(C++20 前)
constexpr basic_string& replace( size_type pos, size_type count, const T& t,
以新字符串替换 [pos, pos + count)
或 [first, last)
所指示的 string 部分。
新字符串可以是下列之一:
str
;str
的子串 [pos2, pos2 + count2)
,除了若 count2==npos 或若它可能越过 str.size() ,则使用 [pos2, str.size())
。[first2, last2)
中的字符。
若 |
(C++11 前) |
此重载仅若 |
(C++11 起) |
[cstr, cstr + count2)
中的字符;[cstr, cstr + Traits::length(cstr))
中的字符;ch
的 count2
个副本;ilist
中的字符;t
转换而来的 string_view sv
的字符。这些重载仅若std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议t
转换而得的字符串视图 sv
的子视图 [pos2, pos2 + count2)
,除了若 count2==npos 或它将越过 sv.size() ,则使用 [pos2, sv.size())
。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议。参数
pos | - | 将被替换的子串起始位置 |
count | - | 将被替换的子串长度 |
first, last | - | 将被替换的字符范围 |
str | - | 用于替换的 string |
pos2 | - | 用于替换的子串起始位置 |
count2 | - | 用于替换的字符数 |
cstr | - | 指向用于替换的字符串的指针 |
ch | - | 用于替换的字符值 |
first2, last2 | - | 用于替换的字符范围 |
ilist | - | 拥有用于替换的字符的 initializer_list |
t | - | 拥有用于替换的字符的对象(可转换为 std::basic_string_view ) |
返回值
*this
异常
若 pos > length()
或 pos2 > str.length()
则为 std::out_of_range
若产生的 string 将超出最大可能 string 长度( max_size() - 1
)则为 std::length_error 。
任何情况下,若因任何原因抛出异常,则此函数无效果(强异常保证)。 (C++11 起)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
DR | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2946 | C++17 | string_view 重载在某些情况下导致歧义
|
通过使之为模板来避免 |
示例
#include <iostream> #include <string> int main() { std::string str("The quick brown fox jumps over the lazy dog."); str.replace(10, 5, "red"); // (5) str.replace(str.begin(), str.begin() + 3, 1, 'A'); // (6) std::cout << str << '\n'; }
输出:
A quick red fox jumps over the lazy dog.