Skip to content

Instantly share code, notes, and snippets.

@addam
Created September 20, 2017 05:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save addam/4f8f568f55e3f1a124eb68225fefdbc0 to your computer and use it in GitHub Desktop.
Save addam/4f8f568f55e3f1a124eb68225fefdbc0 to your computer and use it in GitHub Desktop.
C++ move_if: remove elements from a sequence and move them over elsewhere
template<typename It, typename OutIt, typename UnaryPredicate>
It move_if(It begin, It end, OutIt dst, UnaryPredicate pred)
{
It last = end;
while (begin != last) {
if (pred(*begin)) {
*dst++ = std::move(*begin);
std::swap(*begin, *(--last));
} else {
++begin;
}
}
return last;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment