I have an array of structures that contain multiple variables:
struct test_case {
const int input1;
//...
const int output;
};
test_case tc[] = {
{0, /**/ 1},
// ...
{99, /**/ 17}
};
int tc_size = sizeof(tc) / sizeof(*tc);
and I want to extract a vector of the output
s so I can compare them to another array via BOOST_CHECK_EQUAL_COLLECTIONS
.
I came up with this:
struct extract_output {
int operator()(test_t &t) {
return t.output;
}
}
std::vector<int> output_vector;
std::transform(test_cases,
test_cases + tc_size,
back_inserter(output_vector),
extract_output());
but it seems like I should be able to do this without a functor/struct for each type.
Is there a quicker way to extract a vector/array of one variable from a struct? I tried using boost::lambda, but this didn't work:
std::transform(test_cases,
test_cases + tc_size,
back_inserter(output_vector),
_1.output);
Apparently operator.()
cannot be used on lambda variables... what should I use? boost::bind?