public member function

std::forward_list::unique

<forward_list>
(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.

For (2), the version accepting a binary predicate, a specific comparison function to determine the "uniqueness" of an element can be specified. 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 list if the predicate returns true.

The elements removed have their destructors called and their iterators and references become invalid.

A global algorithm function, unique, exists with a similar behavior but operating in a range (between two iterators).

Paramaters

binary_pred
Binary predicate that, taking two values of the same type than those contained in the list object as argument, returns true to remove the element passed as first argument from the container, and false otherwise.
BinaryPredicate can either be a function pointer type or a class that overloads operator().

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 << std::endl;

  return 0;
}


Output:
mylist contains: 3.14 15.2 69.2

Complexity

Linear in container size minus one.

Iterator validity

All the iterators, pointers and references to elements that have not been removed remain valid after this operation and refer to the same elements they were referring to before.

See also