public member function
<forward_list>

std::forward_list::unique

(1)
void unique();
(2)
template <class BinaryPredicate>  void unique (BinaryPredicate binary_pred);
Remove duplicate values
The version with no parameters (1), removes all but the first element from every consecutive group of equal elements in the container.

Notice that an element is only removed from the forward_list container if it compares equal to the element immediately preceding it. Thus, this function is especially useful for sorted lists.

The second version (2), takes as argument a specific comparison function that determine the "uniqueness" of an element. In fact, any behavior can be implemented (and not only an equality comparison), but notice that the function will call binary_pred(*i,*(i-1)) for all pairs of elements (where i is an iterator to an element, starting from the second) and remove i from the forward_list if the predicate returns true.

The elements removed are destroyed.

Paramaters

binary_pred
Binary predicate that, taking two values of the same type than those contained in the forward_list, returns true to remove the element passed as first argument from the container, and false otherwise.
This shall be a function pointer or a function object.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// forward_list::unique
#include <iostream>
#include <cmath>
#include <forward_list>

// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }

// a binary predicate implemented as a class:
class is_near_class
{
public:
  bool operator() (double first, double second)
  { return (fabs(first-second)<5.0); }
} is_near_object;

int main ()
{

  std::forward_list<double> mylist = { 15.2, 73.0, 3.14, 15.85, 69.5,
                                       73.0, 3.99, 15.2, 69.2,  18.5 };

  mylist.sort();                       //   3.14,  3.99, 15.2, 15.2, 15.85
                                       //  18.5,  69.2,  69.5, 73.0, 73.0

  mylist.unique();                     //   3.14,  3.99, 15.2, 15.85
                                       //  18.5,  69.2,  69.5, 73.0

  mylist.unique (same_integral_part);  //  3.14, 15.2, 18.5,  69.2, 73.0

  mylist.unique (is_near_object);      //  3.14, 15.2, 69.2

  std::cout << "mylist contains:";
  for (double& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Output:
mylist contains: 3.14 15.2 69.2


Complexity

Linear in container size minus one.

Iterator validity

Iterators, pointers and references referring to elements removed by the function are invalidated.
All other iterators, pointers and references keep their validity.

Data races

The container is modified.
The elements removed are modified. Concurrently accessing or modifying other elements is safe, although iterating through the container is not.

Exception safety

If binary_pred or the comparison of elements is guaranteed to not throw, the function never throws exceptions (no-throw guarantee).
Otherwise, if an exception is thrown, the container is left in a valid state (basic guarantee).

See also