名稱空間
變體
操作

std::basic_string<CharT,Traits,Allocator>::replace

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
basic_string& replace( size_type pos, size_type count,
                       const basic_string& str );
(1) (C++20 起為 constexpr)
basic_string& replace( const_iterator first, const_iterator last,
                       const basic_string& str );
(2) (C++20 起為 constexpr)
(3)
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 );
(C++14 起)
(C++20 起為 constexpr)
basic_string& replace( size_type pos, size_type count,
                       const CharT* cstr, size_type count2 );
(4) (C++20 起為 constexpr)
basic_string& replace( const_iterator first, const_iterator last,
                       const CharT* cstr, size_type count2 );
(5) (C++20 起為 constexpr)
basic_string& replace( size_type pos, size_type count,
                       const CharT* cstr );
(6) (C++20 起為 constexpr)
basic_string& replace( const_iterator first, const_iterator last,
                       const CharT* cstr );
(7) (C++20 起為 constexpr)
basic_string& replace( size_type pos, size_type count,
                       size_type count2, CharT ch );
(8) (C++20 起為 constexpr)
basic_string& replace( const_iterator first, const_iterator last,
                       size_type count2, CharT ch );
(9) (C++20 起為 constexpr)
template< class InputIt >

basic_string& replace( const_iterator first, const_iterator last,

                       InputIt first2, InputIt last2 );
(10) (C++20 起為 constexpr)
basic_string& replace( const_iterator first, const_iterator last,
                       std::initializer_list<CharT> ilist );
(11) (C++11 起)
(C++20 起為 constexpr)
template< class StringViewLike >

basic_string& replace( size_type pos, size_type count,

                       const StringViewLike& t );
(12) (C++17 起)
(C++20 起為 constexpr)
template< class StringViewLike >

basic_string& replace( const_iterator first, const_iterator last,

                       const StringViewLike& t );
(13) (C++17 起)
(C++20 起為 constexpr)
template< class StringViewLike >

basic_string& replace( size_type pos, size_type count,
                       const StringViewLike& t,

                       size_type pos2, size_type count2 = npos );
(14) (C++17 起)
(C++20 起為 constexpr)

替換範圍 [begin() + posstd::min(pos + count, size()))[firstlast) 中的字元,替換為給定字元。

1,2) 這些字元被 str 替換。
3) 這些字元被 str 的子字串 [pos2std::min(pos2 + count2, str.size())) 替換。
4,5) 這些字元被範圍 [cstrcstr + count2) 中的字元替換。
如果 [cstrcstr + count2) 不是 有效範圍,則行為未定義。
6,7) 這些字元被範圍 [cstrcstr + Traits::length(cstr)) 中的字元替換。
8,9) 這些字元被 count2ch 替換。
10) 這些字元被範圍 [first2last2) 中的字元替換,如同透過 replace(first, last, basic_string(first2, last2, get_allocator()))
11) 這些字元被 ilist 中的字元替換。
12,13)t 隱式轉換為字串檢視 sv,如同透過 std::basic_string_view<CharT, Traits> sv = t;,然後這些字元被 sv 中的字元替換。
只有當 std::is_convertible_v<const StringViewLike&,
                      std::basic_string_view<CharT, Traits>>
truestd::is_convertible_v<const StringViewLike&, const CharT*>false 時,這些過載才參與過載決議。
14)t 隱式轉換為字串檢視 sv,如同透過 std::basic_string_view<CharT, Traits> sv = t;,然後這些字元被子檢視 sv.substr(pos2, count2) 中的字元替換。
只有當 std::is_convertible_v<const StringViewLike&,
                      std::basic_string_view<CharT, Traits>>
truestd::is_convertible_v<const StringViewLike&, const CharT*>false 時,此過載才參與過載決議。

如果 [begin()first)[firstlast) 不是 有效範圍,則行為未定義。

目錄

[edit] 引數

pos - 將被替換的子字串的起始位置
count - 將被替換的子字串的長度
first, last - 將被替換的字元範圍
str - 用於替換的字串
pos2 - 用於替換的子字串的起始位置
count2 - 用於替換的字元數量
cstr - 指向用於替換的字元字串的指標
ch - 用於替換的字元值
first2, last2 - 用於替換的字元範圍
ilist - 包含用於替換的字元的初始化列表
t - 可轉換為 std::basic_string_view 的物件,包含用於替換的字元
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。

[edit] 返回值

*this.

[edit] 異常

1) 如果 pos > size() 則丟擲 std::out_of_range
3) 如果 pos > size()pos2 > str.size() 則丟擲 std::out_of_range
4,6,8) 如果 pos > size() 則丟擲 std::out_of_range
12,14) 如果 pos > size() 則丟擲 std::out_of_range

如果操作會導致 size() 超過 max_size(),則丟擲 std::length_error

如果因任何原因丟擲異常,這些函式沒有效果(強異常安全保證)。

[edit] 示例

[edit] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 847 C++98 沒有異常安全保證 添加了強異常安全保證
LWG 1323 C++98 firstlast 的型別是 iterator 改為 const_iterator
LWG 2946 C++17 過載 (12,13) 在某些情況下會導致歧義 透過將其變為模板來避免

[edit] 參閱

用字元範圍替換字串的指定部分
(公共成員函式) [編輯]
用格式化的替換文字替換正則表示式的出現
(函式模板) [編輯]
用另一個值替換所有滿足特定條件的值
(函式模板) [編輯]