Houman

February 2nd, 2008, 08:56 PM

hi there;

I have decided to use the C++ standard libraries algorithms for the first time,

so the first problem i was going to solve with it was basically counting the number of "trues" in a vector of bools, simple enough, but its confusing me;

here is the code that i have:

int countTrues(const vector<bool>& vec)

{

return (int) count_if (vec.begin(), vec.end(), isTrue);

}

bool isTrue(bool val)

{

return val;

}

now with my good old C i could have written this in one line, so is there any way not to have to use an additional function? is there any way to use the algorithms to solve this problem in less number of lines?

regards

Houman

I have decided to use the C++ standard libraries algorithms for the first time,

so the first problem i was going to solve with it was basically counting the number of "trues" in a vector of bools, simple enough, but its confusing me;

here is the code that i have:

int countTrues(const vector<bool>& vec)

{

return (int) count_if (vec.begin(), vec.end(), isTrue);

}

bool isTrue(bool val)

{

return val;

}

now with my good old C i could have written this in one line, so is there any way not to have to use an additional function? is there any way to use the algorithms to solve this problem in less number of lines?

regards

Houman