std::unique_copy
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class InputIt, class OutputIt > ForwardIt unique_copy( InputIt first, InputIt last, |
(1) | |
template< class InputIt, class OutputIt, class BinaryPredicate > ForwardIt unique_copy( InputIt first, InputIt last, |
(2) | |
的元素复制的范围内
[first, last)
,在这样一种方式,有没有连续相等的元素d_first
开始到另一个范围。只有相等的元素的每个组的第一个元素被复制。的第一个版本使用operator==
比较的元素,第二个版本使用给定的二元谓词p
.原文:
Copies the elements from the range
[first, last)
, to another range beginning at d_first
in such a way that there are no consecutive equal elements. Only the first element of each group of equal elements is copied. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
.目录 |
[编辑] 参数
first, last | - | 元素的范围内进行处理
|
d_first | - | 的目标范围的开头
原文: the beginning of the destination range |
p | - | binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。
| ||
-OutputIt 必须满足 OutputIterator 的要求。
| ||
-The type of dereferenced InputIt must meet the requirements of CopyAssignable .
| ||
-The type of dereferenced InputIt must meet the requirements of CopyConstructible . if neither InputIt nor OutputIt satisfies ForwardIterator
|
[编辑] 返回值
过去的最后写入的元素的元素的输出迭代器
原文:
Output iterator to the element past the last written element
[编辑] 可能的实现
版本一 |
---|
template<class ForwardIt, class OutputIt> ForwardIt unique_copy(ForwardIt first, ForwardIt last, OutputIt d_first) { if (first == last) return d_first; *d_first = *first; while (++first != last) { if (!(*d_first == *first)) { *(++d_first) = *first; } } return ++d_first; } |
版本二 |
template<class ForwardIt, class OutputIt, class BinaryPredicate> ForwardIt unique_copy(ForwardIt first, ForwardIt last, OutputIt d_first, BinaryPredicate p) { if (first == last) return d_first; *d_first = *first; while (++first != last) { if (!p(*result, *first)) { *(++d_first) = *first; } } return ++d_first; } |
[编辑] 示例
下面的程序修剪所有的多个连续的空格在一个常量字符串,并输出结果
原文:
The following program trims all multiple consecutive spaces in a const string and prints the result
#include <string> #include <iostream> #include <algorithm> #include <iterator> int main() { std::string s1 = "The string with many spaces!"; std::cout << "before: " << s1 << '\n'; std::string s2; std::unique_copy(s1.begin(), s1.end(), std::back_inserter(s2), [](char c1, char c2){ return c1 == ' ' && c2 == ' '; }); std::cout << "after: " << s2 << '\n'; }
输出:
before: The string with many spaces! after: The string with many spaces!
[编辑] 复杂度
线性
first
和last
之间的距离原文:
linear in the distance between
first
and last
[编辑] 另请参阅
查找彼此相邻的两个相同(或其它的关系)的元素 (函数模板) | |
删除区间内连续重复的元素 (函数模板) |