In the below code I want to create a function count
which counts the number of integers/strings which qualifies a match criteria from a vector of integers/strings.
But I am not clear about how to write the function definition.
#include <iostream>
#include <vector>
using namespace std;
bool match(int x) {
return (x % 2 == 0);
}
bool match(string x) {
return (x.length <= 3);
}
template <typename T>
int count(vector<T>& V, bool (*test)(<T>))
{
int tally = 0;
for (int i = 0; i < V.size(); i++) {
if (test(V[i])) {
tally++;
}
}
return tally;
}
int main()
{
vector <int> nums;
vector <string> counts;
nums.push_back(2);
nums.push_back(4);
nums.push_back(3);
nums.push_back(5);
counts.push_back("one");
counts.push_back("two");
counts.push_back("three");
counts.push_back("four");
cout << count(nums, match) << endl;
cout << count(counts, match) << endl;
}
How should the prototype be written ? I realize the error is at the line
int count (vector<T> &V , bool (*test)(<T>) )