std::remove_copy, std::remove_copy_if
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class InputIt, class OutputIt, class T > OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first, |
(1) | |
template< class InputIt, class OutputIt, class UnaryPredicate > OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first, |
(2) | |
Copies elements from the range [first, last)
, to another range beginning at d_first
, omitting the elements which satisfy specific criteria. The first version ignores the elements that are equal to value
, the second version ignores the elements for which predicate p
returns true. Source and destination ranges cannot overlap.
目录 |
[编辑] 参数
first, last | - | 元素的范围内,要复制的复本
|
d_first | - | the beginning of the destination range. |
value | - | the value of the elements not to copy |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。
| ||
-OutputIt 必须满足 OutputIterator 的要求。
|
[编辑] 返回值
过去的最后一个元素的元素的迭代器,复制.
原文:
Iterator to the element past the last element copied.
[编辑] 复杂度
究竟
last - first
的谓词中的应用.原文:
Exactly
last - first
applications of the predicate.[编辑] 可能的实现
版本一 |
---|
template<class InputIt, class OutputIt, class T> 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; } |
版本二 |
template<class InputIt, class OutputIt, class UnaryPredicate> OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p) { for (; first != last; ++first) { if (!p(*first)) { *d_first++ = *first; } } return d_first; } |
[编辑] 示例
The following code outputs a string while erasing the spaces on the fly.
#include <algorithm> #include <iterator> #include <string> #include <iostream> int main() { std::string str = "Text with some spaces"; std::cout << "before: " << str << "\n"; std::cout << "after: "; std::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), ' '); std::cout << '\n'; }
输出:
before: Text with some spaces after: Textwithsomespaces
[编辑] 另请参阅
删除满足特定条件的元素 (函数模板) |