Question Details

No question body available.

Tags

c++ std-ranges

Answers (2)

January 22, 2026 Score: 7 Rep: 47,194 Quality: Medium Completeness: 70%

Range-v3 has views::unique. However, the standard library currently does not have a similar one, although it is marked as Tier 2 on P2760.

However, you can assemble something similar using existing adaptors:

/ const / auto ret = v | std::views::chunkby(std::ranges::equalto{})
                         | std::views::transform(std::views::take(1))
                         | std::views::join;

Demo

Note that const-qualified cannot be used because chunkbyview is not const-iterable.

January 22, 2026 Score: 4 Rep: 43,927 Quality: Low Completeness: 40%

Non vector destructive solution is possible with std::span.

#include 

#include #include #include

int main() { std::vector v{1, 2, 1, 1, 3, 3, 3, 4, 5, 4}; fmt::println("{}", v); // [1, 2, 1, 1, 3, 3, 3, 4, 5, 4]

const std::span ret(v.begin(), std::ranges::unique(v).begin()); fmt::println("{}", ret); // [1, 2, 1, 3, 4, 5, 4] }