Created
March 24, 2019 21:39
-
-
Save giulioz/7fca2bd215040b595bcae07e95c31903 to your computer and use it in GitHub Desktop.
(C++) Creates a new vector from a generic array and a predicate, like JS's filter
This file contains hidden or 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 Array, typename Predicate> | |
auto filterArray(Array arr, Predicate p) { | |
using arrElemType = typename std::remove_reference<decltype(arr[0])>::type; | |
auto result = std::vector<arrElemType>(); | |
for (const auto el : arr) { | |
if (p(el)) result.push_back(el); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment