您的位置:首页 > 产品设计 > UI/UE

std::unique函数的使用

2016-06-23 00:00 1031 查看
(当你已经有一个有序列表,再配合此函数实现列表元素唯一性也是蛮好的)

function template

<algorithm>

std::unique

equality (1)
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
predicate (2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
Remove consecutive duplicates(副本) in range

Removes all but the first element (note:是否应该加个s)from every consecutive group of equivalent elements in the range
[first,last)
.

The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container): The removal is done by replacing the duplicate elements by the next element that is not a duplicate, and signaling the new size of the shortened range by returning an iterator to ( note:注意介词的用法) the element that should be considered its new past-the-end element.(相当于stl中end函数返回的那个迭代器所指向的 位置)

The relative order of the elements not removed is preserved(note:即原来的元素的顺序具有稳定性), while the elements between the returned iterator and last are left in a valid but unspecified state(note:由于删除了一些重复元素,造成的容器尾部空闲空间中的值可能处于无效且不确定的状态).

The function uses
operator==
to compare the pairs of elements (or pred, in version (2)).

The behavior of this function template is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first==last) return last;   //如果范围内只有一个元素,则直接返回。

ForwardIterator result = first;
while (++first != last)
//当first达到最后一个元素的位置时,跳出。如果last位置是重复元素是否会被处理?不会,实际上这个函数的last参数表示的边界是开区间。
{
//在使用指针的循环中经常出现快慢两种指针,这两种指针的配合往往可以产生各种高效的算法。比如只遍历一次求倒数第K个元素的问题。
if (!(*result == *first))  // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}
Parameters

first, last

Forward iterators to the initial and final positions of the sequence of move-assignable elements. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed byfirst but not the element pointed by last.

pred

Binary function that accepts two elements in the range as argument, and returns a value convertible to
bool
. The value returned indicates whether both arguments are considered equivalent (if
true
, they are equivalent and one of them is removed).
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return value

An iterator to the element that follows the last element not removed.
The range between first and this iterator includes all the elements in the sequence that were not considered duplicates.

Example

1
2
3
4
5
6
7
8
9
10
11
12
1314
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// unique algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::unique, std::distance
#include <vector>       // std::vector

bool myfunction (int i, int j) {
return (i==j);
}

int main () {
int myints[] = {10,20,20,20,30,30,20,20,10};           // 10 20 20 20 30 30 20 20 10
std::vector<int> myvector (myints,myints+9);

// using default comparison:
std::vector<int>::iterator it;
it = std::unique (myvector.begin(), myvector.end());   // 10 20 30 20 10 ?  ?  ?  ?
//注意unique函数只能保证最终结果每个元素与它连续的元素不相同,所以最终出现10,20,10                                                      //这样的结果也不要大惊小怪。               ^

//注意distance的用法。
myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10

// using predicate comparison:
std::unique (myvector.begin(), myvector.end(), myfunction);   // (no changes)

// print out content:
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

return 0;
}
Output:

myvector contains: 10 20 30 20 10
Complexity

For non-empty ranges, linear in one less than the distance between first and last: Compares each pair of consecutive elements, and possibly performs assignments on some of them.

Data races

The objects in the range
[first,last)
are accessed and potentially modified.

Exceptions

Throws if any of pred, the element comparisons, the element assignments or the operations on iterators throws.
Note that invalid arguments cause undefined behavior.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: