Removing elements marked for removal with Ranges-V3

  c++, c++17, erase-remove-idiom

I’ve got two vectors:

//avoiding bool specialization
std::vector<int> remove_flags;
std::vector<MyData> data; 

The remove_flags vector contains an array of flags of the exact same size as data, each flag is either 0, or 1, where 1 means the data should be removed.

Doing this manually is annoying, and I wanted to use ranges-v3 for this. I’m currently using C++17.

After looking through the documentation, I don’t think I’ve found a solution, the closest thing I could come up with is:

auto result = ranges::views::zip(remove_flags, data) | ranges::actions::remove_if([](std::pair<const int&, const MyData&> pair){
    return pair.first != 0;
});

remove_marker_flags.erase(result.first);
markers.erase(result.second);

But actions cannot operate on the view zip, so this does not compile. If I switch ranges::actions::remove_if to ranges::views::remove_if a strange views object is returned, presumably one that has not actually performed the std::remove equivalent operation on the two vectors.

I could use contaner_to but that would involve a copy, and I don’t want to pay that kind of unnecessary penalty for convenience. I’ve seen this accomplished in boost where actual zip pair iterators can be used to return two separate removal results.

Is this kind of pattern possible in ranges v3?

Source: Windows Questions C++

LEAVE A COMMENT