std::unique
来自cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Defined in header <algorithm>
|
||
template< class ForwardIt > ForwardIt unique( ForwardIt first, ForwardIt last ); |
(1) | |
template< class ForwardIt, class BinaryPredicate > ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p ); |
(2) | |
删除所有连续重复的元素的范围
[first, last)
。卸下做是通过移动的范围内,在需要的时候在这样一种方式,要被擦除的元素将被覆盖。只有相等的元素的每个组中的第一个元素的在左边。元素之间的旧的和新的结束或范围保持不变。的第一个版本使用operator==
比较的元素,第二个版本使用给定的二元谓词p
.Original:
Removes all consecutive duplicate elements from the range
[first, last)
. Removing is done by shifting the range when needed in such a way that elements to be erased are overwritten. Only the first element in each group of equal elements is left. The elements between the old and the new end or the range are left intact. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 参数
first, last | - | 元素的范围内进行处理
Original: the range of elements to process The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
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. |
Type requirements | ||
-ForwardIt must meet the requirements of ForwardIterator .
| ||
-The type of dereferenced ForwardIt must meet the requirements of MoveAssignable .
|
[编辑] 返回值
前向迭代器新的范围
Original:
Forward iterator to the new end of the range
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 可能的实现
First version |
---|
template<class ForwardIt> ForwardIt unique(ForwardIt first, ForwardIt last) { if (first == last) return last; ForwardIt result = first; while (++first != last) { if (!(*result == *first)) { *(++result) = *first; } } return ++result; } |
Second version |
template<class ForwardIt, class BinaryPredicate> ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPredicate p) { if (first == last) return last; ForwardIt result = first; while (++first != last) { if (!p(*result, *first)) { *(++result) = *first; } } return ++result; } |
[编辑] 为例
下面的代码将删除所有连续相等的元素的向量整数.
Original:
The following code removes all consecutive equivalent elements from a vector of integers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> v{1, 2, 2, 2, 3, 3, 2, 2, 1}; std::vector<int>::iterator last; last = std::unique(v.begin(), v.end()); // 1 2 3 2 1 3 2 2 1 // ^ for (std::vector<int>::iterator it = v.begin(); it != last; ++it) { std::cout << *it << " "; } std::cout << "\n"; }
Output:
1 2 3 2 1
[编辑] 复杂性
线性
first
和last
之间的距离Original:
linear in the distance between
first
and last
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 另请参阅
查找彼此相邻的两个相同(或其它的关系)的元素 (函数模板) | |
删除区间内连续重复的元素并复制 (函数模板) |