Skip to content

Instantly share code, notes, and snippets.

@giulioz
Created March 24, 2019 21:39
Show Gist options
  • Save giulioz/7fca2bd215040b595bcae07e95c31903 to your computer and use it in GitHub Desktop.
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
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