std::replace, std::replace_if
定義於標頭檔案 <algorithm> |
||
(1) | ||
template< class ForwardIt, class T > void replace( ForwardIt first, ForwardIt last, |
(C++20 起為 constexpr) (直到 C++26) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type > |
(C++26 起) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class T > void replace( ExecutionPolicy&& policy, |
(C++17 起) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(C++26 起) | |
(3) | ||
template< class ForwardIt, class UnaryPred, class T > void replace_if( ForwardIt first, ForwardIt last, |
(C++20 起為 constexpr) (直到 C++26) |
|
template< class ForwardIt, class UnaryPred, class T = typename std::iterator_traits |
(C++26 起) | |
(4) | ||
template< class ExecutionPolicy, class ForwardIt, class UnaryPred, class T > |
(C++17 起) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class UnaryPred, |
(C++26 起) | |
用 new_value 替換範圍 [
first,
last)
中所有滿足特定條件的元素。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 為 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 為 true。 |
(C++20 起) |
如果 *first = new_value 無效(直至 C++20)new_value 對 first 不可寫入(C++20 起),則程式非良構。
目錄 |
[編輯] 引數
first, last | - | 定義要處理的元素範圍的迭代器對 |
old_value | - | 要替換的元素的值 |
policy | - | 要使用的 執行策略 |
p | - | 一元謂詞,若元素值應被替換則返回 true。 表示式 p(v) 必須可轉換為 bool,對於型別為 (可能為 const) |
new_value | - | 用於替換的值 |
型別要求 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-UnaryPred 必須滿足 Predicate 的要求。 |
[編輯] 複雜度
給定 N 為 std::distance(first, last)
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 若作為演算法一部分呼叫的函式丟擲異常且
ExecutionPolicy
為 標準策略之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 注意
因為演算法透過引用接受 old_value 和 new_value,如果其中任何一個是對範圍 [
first,
last)
中元素的引用,則可能產生意外行為。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 演算法的列表初始化 (1-4) |
[編輯] 可能的實現
replace (1) |
---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> void replace(ForwardIt first, ForwardIt last, const T& old_value, const T& new_value) { for (; first != last; ++first) if (*first == old_value) *first = new_value; } |
replace_if (3) |
template<class ForwardIt, class UnaryPred, class T = typename std::iterator_traits<ForwardIt>::value_type> void replace_if(ForwardIt first, ForwardIt last, UnaryPred p, const T& new_value) { for (; first != last; ++first) if (p(*first)) *first = new_value; } |
[編輯] 示例
#include <algorithm> #include <array> #include <complex> #include <functional> #include <iostream> void println(const auto& seq) { for (const auto& e : seq) std::cout << e << ' '; std::cout << '\n'; } int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; // Replace all occurrences of 8 with 88. std::replace(s.begin(), s.end(), 8, 88); println(s); // Replace all values less than 5 with 55. std::replace_if(s.begin(), s.end(), std::bind(std::less<int>(), std::placeholders::_1, 5), 55); println(s); std::array<std::complex<double>, 2> nums{{{1, 3}, {1, 3}}}; #ifdef __cpp_lib_algorithm_default_value_type std::replace(nums.begin(), nums.end(), {1, 3}, {4, 2}); #else std::replace(nums.begin(), nums.end(), std::complex<double>{1, 3}, std::complex<double>{4, 2}); #endif println(nums); }
輸出
5 7 4 2 88 6 1 9 0 3 5 7 55 55 88 6 55 9 55 55 (4,2), (4,2)
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 283 | C++98 | 要求 T 為 CopyAssignable(對於 replace 為 EqualityComparable),但 ForwardIt 的值型別並非總是 T ,且 T 並非總是可寫入 ForwardIt 。要求 *first = new_value |
有效 而不是 |
[編輯] 參閱
複製一個範圍,同時用另一個值替換滿足特定條件的元素 (函式模板) | |
(C++20)(C++20) |
用另一個值替換所有滿足特定條件的值 (演算法函式物件) |