std::forward_list::unique
来自cppreference.com
< cpp | container | forward list
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
void unique(); |
(1) | (C++11 起) |
template< class BinaryPredicate > void unique( BinaryPredicate p ); |
(2) | (C++11 起) |
删除所有重复的元素从容器中的“连续”。只有相等的元素的每个组中的第一个元素的在左边。的第一个版本使用
operator==
比较的元素,第二个版本使用给定的二元谓词p
.原文:
Removes all consecutive duplicate elements from the container. Only the first element in each group of equal elements is left. The first version uses
operator==
to compare the elements, the second version uses the given binary predicate p
.目录 |
[编辑] 参数
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. |
[编辑] 返回值
(无)
[编辑] 复杂性
线性大小的容器
[编辑] 为例
#include <iostream> #include <forward_list> int main() { std::forward_list<int> x = {1, 2, 2, 3, 3, 2, 1, 1, 2}; std::cout << "contents before:"; for (auto val : x) std::cout << ' ' << val; std::cout << '\n'; x.unique(); std::cout << "contents after unique():"; for (auto val : x) std::cout << ' ' << val; std::cout << '\n'; return 0; }
输出:
contents before: 1 2 2 3 3 2 1 1 2 contents after unique(): 1 2 3 2 1 2
[编辑] 另请参阅
删除区间内连续重复的元素 (函数模板) |