Exercise: Generic Filter

Base upon Exercise: Filter Odd Elements and/or Exercise: Filter Elements Greater Than 10, write a filter that lets the user supply a function that decides whether an alement is filtered of not.

Hint: the decision-function’s prototype is bool func(element). The parameter type of the filter() function for that function is bool(*func)(int). (func is the name of the passed function as it is used inside the filter() body.)

#include <gtest/gtest.h>

#include <filter.h>                               // <--- implement this
#include <vector>

using namespace std;

bool is_odd(int elem)
{
    return elem % 2 == 1;
}

TEST(stl_exercises_suite, filter)
{
    const vector<int> input = {42, 3, 42, 5, 6, 7, 5, 3, 666};
    const vector<int> orig_input = input;

    const vector<int> output = filter(input, is_odd);   // <--- implement this

    const vector<int> required_output = {3, 5, 7, 5, 3};
    ASSERT_EQ(output, required_output);
    ASSERT_EQ(input, orig_input);                 // <--- function must not modify input
}