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() { // 即时擦除井号字符 '#'。 std::string str = "#返回#值#优化"; std::cout << "擦除前:" << std::quoted(str) << "\n"; std::cout << "擦除后:\""; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), '#'); std::cout << "\"\n"; // 即时擦除 {1, 3} 值。 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 被推导 #else std::complex<double>{1, 3}); #endif }
輸出:
擦除前:#返回#值#优化 擦除后:返回值优化 (2,2)(4,8)
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 出版時的行為 | 正確行為 |
---|---|---|---|
LWG 779 | C++98 | T 需要是可相等比較 (EqualityComparable) 的,但是 ForwardIt 的值類型不一定是 T
|
改成要求 *d_first = *first 合法 |
[編輯] 參閱
移除滿足特定條件的元素 (函數模板) | |
(C++11) |
複製範圍中元素到新位置 (函數模板) |
(C++11) |
複製範圍並將元素分為兩組 (函數模板) |
(C++20)(C++20) |
複製範圍並忽略滿足特定條件的元素 (算法函數對象) |