std::hive<T,Allocator>::unique
template< class BinaryPredicate = std::equal_to<T> >
size_type unique( BinaryPredicate binary_pred = BinaryPredicate() );
|
(since C++26) | |
Removes all consecutive duplicate elements from the container. Only the first element in each group of equal elements is left.
Formally, for a nonempty hive, erases all elements referred to by the iterator i in the [begin() + 1, end()) for which p(*i, *(i - 1)) is true.
Invalidates references, pointers, and iterators referring to the erased elements.
If the last element in *this is erased, also invalidates the end() iterator.
If binary_pred does not establish an equivalence relation, the behavior is undefined.
Parameters
| 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:
While the signature does not need to have |
| Type requirements | ||
-BinaryPredicate must meet the requirements of BinaryPredicate.
| ||
Return value
The number of removed elements.
Complexity
If empty() is true, no comparison is performed.
Otherwise, given N as std::distance(begin(), end()):
exactly N-1 applications of the predicate p.
Example
import std;
int main()
{
std::hive<int> c{1, 2, 2, 3, 3, 2, 1, 1, 2};
std::println("Before unique(): {}", c);
const auto count1 = c.unique();
std::println("After unique(): {} ({} elements removed)", c, count1);
c = {1, 2, 12, 23, 3, 2, 51, 1, 2, 2};
std::println("Before unique(pred): {}", c);
auto pred = [mod = 10](int x, int y) { return (x % mod) == (y % mod); };
const auto count2 = c.unique(pred);
std::println("After unique(pred): {} ({} elements removed)", c, count2);
}
Output:
Before unique(): [1, 2, 2, 3, 3, 2, 1, 1, 2]
After unique(): [1, 2, 3, 2, 1, 2] (3 elements removed)
Before unique(pred): [1, 2, 12, 23, 3, 2, 51, 1, 2, 2]
After unique(pred): [1, 2, 23, 2, 51, 2] (4 elements removed)
See also
| removes consecutive duplicate elements in a range (function template & algorithm function object) | |
(C++20) |