Przestrzenie nazw
Warianty

std::forward_list::unique

Z cppreference.com
<tbody> </tbody>
void unique();
(1) (od C++11)
template< class BinaryPredicate > void unique( BinaryPredicate p );
(2) (od C++11)

Usuwa wszystkie następujące po sobie duplikaty elementów z kontenera. Tylko pierwszy element z koądej grupy równych elementów pozostaje w kontenerze. Pierwsza wersja wykorzystuje operator== do porównywania elementów, druga wykorzystuje do tego podany przez użytkownika predykat binarny p.

Parametry

p - predykat binarny zwracający ​true jeśli elementy powinny być traktowane jako równe.

Sygnatura funkcji - predykatu powinna być równoważna z następującą:

bool pred(const Type1 &a, const Type2 &b);

Sygnatura nie musi zawierać parametru const &, ale funkcja nie może modyfikować przekazanych do niej elementów.
The types Type1 and Type2 must be such that an object of type forward_list<T,Allocator>::const_iterator can be dereferenced and then implicitly converted to both of them. ​

Zwracana wartość

(brak)

Złożoność

Liniowa względem rozmiaru kontenera

Przykład

#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;
}

Wynik:

contents before: 1 2 2 3 3 2 1 1 2
contents after unique(): 1 2 3 2 1 2

Zobacz także

usuwa następujące po sobie duplikaty elementów w podanym przedziale
(szablon funkcji) [edit]