Skip to content

Instantly share code, notes, and snippets.

@insaneyilin
Created June 26, 2018 06:18
Show Gist options
  • Save insaneyilin/4355a6f001b82f9d551839d65e70d059 to your computer and use it in GitHub Desktop.
Save insaneyilin/4355a6f001b82f9d551839d65e70d059 to your computer and use it in GitHub Desktop.
C++ Erase elements from containers by indices
// https://coderwall.com/p/jsxrdq/c-erase-elements-from-containers-by-indices
template<typename Cont, typename It>
auto ToggleIndices(Cont &cont, It beg, It end) -> decltype(std::end(cont))
{
int helpIndx(0);
return std::stable_partition(std::begin(cont), std::end(cont),
[&](decltype(*std::begin(cont)) const& val) -> bool {
return std::find(beg, end, helpIndx++) == end;
});
}
std::vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
int ar[] = { 2, 0, 4 };
v.erase(ToggleIndices(v, std::begin(ar), std::end(ar)), v.end());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment