Created
September 20, 2017 05:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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