21

I'm trying to use a c++20 constrained algorithm for the erase-remove idiom:

std::vector<int> v;
v.erase(std::unique(std::begin(v), std::end(v)), std::end(v));

but when I do a simple transformation:

v.erase(std::ranges::unique(v), std::end(v));

I get an error that the arguments to erase don't match:

error: no matching function for call to 'std::vector<int>::erase(std::ranges::borrowed_subrange_t<std::vector<int>&>, std::vector<int>::iterator)'

A similar error is produced if the second argument is std::ranges::end(v).

How can I get this to work?


The question originally used remove instead of unique, but there is an overloaded std::erase for all containers that makes that particular use case less motivating.

3
  • I can't see std::ranges::remove documented anywhere on cppreference, is it part of the standard? Looks like boost ranges has remove but not c++? Oct 10, 2020 at 19:35
  • @AlanBirtles Yes, it's here. It's listed here, on cppreference as well, but there isn't a specific page for it yet.
    – cigien
    Oct 10, 2020 at 19:44
  • @AlanBirtles I switched to ranges::unique anyway, which is also in c++20.
    – cigien
    Oct 10, 2020 at 20:10

3 Answers 3

13

std::ranges::unique (and std::ranges::remove) returns a sub range from the first removed element to the end of the container so you need to use std::begin before passing to std::vector::erase:

v.erase(std::ranges::begin(std::ranges::remove(v, 42)), std::end(v));
v.erase(std::ranges::begin(std::ranges::unique(v)), std::end(v));
7

Another option would be decomposing the subrange returned by std::ranges::remove/unique, and use those iterators:

auto [Beg, End] = std::ranges::remove(v, 42);
v.erase(Beg, End);
9
  • 2
    @康桓瑋 No, it's fine. You just need to include <ranges>.
    – cigien
    Oct 20, 2020 at 3:16
  • 1
    @康桓瑋 I don't think so. std::ranges::subrange is declared in the <ranges> header. I assume that the GCC team decided not to include all of <ranges> in <algorithm>. Maybe they defined the PairLike protocol for subrange in a header that is not accessible by <algorithm>, but I don't see why that wouldn't be standard-compliant.
    – metalfox
    Oct 20, 2020 at 18:05
  • 1
    @康桓瑋 Part of the subrange structured bindings stuff is defined in the <ranges> header itself, while <algorithm> includes <bits/ranges_algo.h>, which includes <bits/ranges_util.h> where subrange is defined.
    – metalfox
    Oct 21, 2020 at 6:04
  • 2
    @metalfox Already filed a report for this. Thank you for your advice.
    – 康桓瑋
    Oct 21, 2020 at 13:41
  • 1
    @康桓瑋 Wow, you got it fixed quickly! Thanks for the report.
    – metalfox
    Oct 21, 2020 at 14:44
6

It doesn't work since std::ranges::remove() returns not iterator but range. But even if you try v.erase(std::ranges::remove(...)) it will not work, because vector does not have erase() overload which takes range as parameter.

Instead, take a look at std::erase() (defined in <vector>). What you need is probably just std::erase(v, 42).

1
  • 1
    This is nice, but it doesn't really answer the question. This only works if I remove elements using an equivalent of remove. It doesn't work if I want to remove elements with an equivalent of unique. I've updated the question.
    – cigien
    Oct 10, 2020 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.