std::remove_copy, std::remove_copy_if
定義於標頭檔案 <algorithm> |
||
(1) | ||
template< class InputIt, class OutputIt, class T > OutputIt remove_copy( InputIt first, InputIt last, |
(C++20 起為 constexpr) (直到 C++26) |
|
template< class InputIt, class OutputIt, class T = typename std::iterator_traits |
(C++26 起) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > |
(C++17 起) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(C++26 起) | |
template< class InputIt, class OutputIt, class UnaryPred > OutputIt remove_copy_if( InputIt first, InputIt last, |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred > |
(4) | (C++17 起) |
將範圍 [
first,
last)
中的元素複製到從 d_first 開始的另一個範圍,同時省略滿足特定條件的元素。
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 起) |
如果 *d_first = *first 無效(C++20 前)*first 不可寫入到 d_first(C++20 起),則程式格式錯誤。
如果源範圍和目標範圍重疊,則行為未定義。
目錄 |
[編輯] 引數
first, last | - | 定義要複製的元素的源 範圍 的迭代器對 |
d_first | - | 目標範圍的開頭 |
value | - | 不復制的元素的值 |
policy | - | 要使用的 執行策略 |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-OutputIt 必須滿足 LegacyOutputIterator 的要求。 | ||
-ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。 | ||
-UnaryPred 必須滿足 Predicate 的要求。 |
[編輯] 返回值
指向最後一個被複制元素之後的元素的迭代器。
[編輯] 複雜度
給定 N 作為 std::distance(first, last)
對於帶有 ExecutionPolicy 的過載,如果 ForwardIt1
的 value_type
不可 MoveConstructible,可能會有效能開銷。
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 如果作為演算法一部分呼叫的函式執行丟擲異常,並且
ExecutionPolicy
是 標準策略 之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為由實現定義。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
remove_copy (1) |
---|
template<class InputIt, class OutputIt, class T = typename std::iterator_traits<InputIt>::value_type> constexpr OutputIt remove_copy(InputIt first, InputIt last, OutputIt d_first, const T& value) { for (; first != last; ++first) if (!(*first == value)) *d_first++ = *first; return d_first; } |
remove_copy_if (3) |
template<class InputIt, class OutputIt, class UnaryPred> constexpr OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred p) { for (; first != last; ++first) if (!p(*first)) *d_first++ = *first; return d_first; } |
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 演算法的列表初始化 (1,2) |
[編輯] 示例
#include <algorithm> #include <complex> #include <iomanip> #include <iostream> #include <iterator> #include <string> #include <vector> int main() { // Erase the hash characters '#' on the fly. std::string str = "#Return #Value #Optimization"; std::cout << "before: " << std::quoted(str) << '\n'; std::cout << "after: \""; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), '#'); std::cout << "\"\n"; // Erase {1, 3} value on the fly. std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}}; std::remove_copy(nums.begin(), nums.end(), std::ostream_iterator<std::complex<double>>(std::cout), #ifdef __cpp_lib_algorithm_default_value_type {1, 3}); // T gets deduced #else std::complex<double>{1, 3}); #endif }
輸出
before: "#Return #Value #Optimization" after: "Return Value Optimization" (2,2)(4,8)
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 779 | C++98 | 要求 T 為 EqualityComparable,但ForwardIt 的值型別不總是 T |
要求 *d_first = *first 是有效的,而不是 |
[編輯] 參閱
移除滿足特定標準的元素 (函式模板) | |
(C++11) |
將一個範圍的元素複製到一個新位置 (函式模板) |
(C++11) |
複製一個範圍,並將元素分成兩組 (函式模板) |
(C++20)(C++20) |
複製一個範圍的元素,忽略那些滿足特定條件的元素 (演算法函式物件) |