unique_by_key
is a generalization of unique
to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last)
that are equal, unique_by_key
removes all but the first element of the group. Similarly, the corresponding values in the range [values_first, values_first + (keys_last - keys_first))
are also removed.
The return value is a pair
of iterators (new_keys_last,new_values_last)
such that no two consecutive elements in the range [keys_first, new_keys_last)
are equal.
This version of unique_by_key
uses operator==
to test for equality and project1st
to reduce values with equal keys.
- Parameters
-
keys_first | The beginning of the key range. |
keys_last | The end of the key range. |
values_first | The beginning of the value range. |
- Returns
- A pair of iterators at end of the ranges
[key_first, keys_new_last)
and [values_first, values_new_last)
.
- Template Parameters
-
- Precondition
- The range
[keys_first, keys_last)
and the range [values_first, values_first + (keys_last - keys_first))
shall not overlap.
The following code snippet demonstrates how to use unique_by_key
to compact a sequence of key/value pairs to remove consecutive duplicates.
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N] = {9, 8, 7, 6, 5, 4, 3};
- See also
- unique
-
unique_by_key_copy
-
reduce_by_key