[first, last), to the destination range beginning at result in such a way that there are no consecutive equal elements. Only the first element of each group of equal elements is copied.[first, last) and [result, result + N) must not overlap. N = ranges::distance(first, last).*(i - 1) and *i are considered equivalent if std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true, where i is an iterator in the range [first + 1, last).r as the range, as if using ranges::begin(r) as first, and ranges::end(r) as last.The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
Parameters
| first, last | - | the source range of elements |
| r | - | the source range of elements |
| result | - | the destination range of elements |
| comp | - | the binary predicate to compare the projected elements |
| proj | - | the projection to apply to the elements |
Return value
{last, result + N}
Complexity
Exactly N - 1 applications of the corresponding predicate comp and no more than twice as many applications of any projection proj.
Possible implementation
See also the implementations in libstdc++ and MSVC STL (and third-party libraries: cmcstl2, NanoRange, and range-v3).
|
Example
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <type_traits>
void print(const auto& rem, const auto& v)
{
using V = std::remove_cvref_t<decltype(v)>;
constexpr bool sep{std::is_same_v<typename V::value_type, int>};
std::cout << rem << std::showpos;
for (const auto& e : v)
std::cout << e << (sep ? " " : "");
std::cout << '\n';
}
int main()
{
std::string s1{"The string with many spaces!"};
print("s1: ", s1);
std::string s2;
std::ranges::unique_copy(
s1.begin(), s1.end(), std::back_inserter(s2),
[](char c1, char c2) { return c1 == ' ' && c2 == ' '; }
);
print("s2: ", s2);
const auto v1 = {-1, +1, +2, -2, -3, +3, -3};
print("v1: ", v1);
std::list<int> v2;
std::ranges::unique_copy(
v1, std::back_inserter(v2),
{}, // default comparator std::ranges::equal_to
[](int x) { return std::abs(x); } // projection
);
print("v2: ", v2);
}s1: The string with many spaces!
s2: The string with many spaces!
v1: -1 +1 +2 -2 -3 +3 -3
v2: -1 +2 -3See also
| (C++20) |
removes consecutive duplicate elements in a range (algorithm function object) |
| (C++20)(C++20) |
copies a range of elements to a new location (algorithm function object) |
| (C++20) |
finds the first two adjacent items that are equal (or satisfy a given predicate) (algorithm function object) |
| creates a copy of some range of elements that contains no consecutive duplicates (function template) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.