Coming back to C++ after years of C# I was wondering what the modern - read: C++11 - way of filtering an array would be, i.e. how can we achieve something similar to this Li
My suggestion for C++ equivalent of C#
var filteredElements = elements.Where(elm => elm.filterProperty == true);
Define a template function to which you pass a lambda predicate to do the filtering. The template function returns the filtered result. eg:
template
vector select_T(const vector& inVec, function predicate)
{
vector result;
copy_if(inVec.begin(), inVec.end(), back_inserter(result), predicate);
return result;
}
to use - giving a trivial examples:
std::vector mVec = {1,4,7,8,9,0};
// filter out values > 5
auto gtFive = select_T(mVec, [](auto a) {return (a > 5); });
// or > target
int target = 5;
auto gt = select_T(mVec, [target](auto a) {return (a > target); });