I have built a generic function which takes a certain container (using an Iterator for its beginning and end) and a Predicate (functor which serves as a some sort of condition check) and counts the number of pairs in that container which are true in the Predicate condition. As the following
//Counts number of pairs in a container that follow the rule of the Predicate
template <typename Iterator, typename Predicate>
int countPairs(const Iterator first, const Iterator last, Predicate pred){
int counter = 0;
for(Iterator current = first; current<last; ++current){
Iterator next(current);
for(++next; next<last; ++next){
if(pred(*current, *next)){
counter++;
}
}
}
return counter;
}
Then, i wanted to use the generic function in order to examine whether a certain vector is sorted. So i've built the following 'Predicate' which checks whether a pair of numbers is sorted:
bool isBigger(int a, int b){
return b < a;
}
Then, i've built a function called which basically uses the two above in order to take a vector and check if its sorted:
bool isSorted(std::vector<int>& v){
int size = v.size();
if(size == 0 || size == 1){
return true;
}
return countPairs(v.begin(), v.end(), isBigger()) == 0;
}
When I try to build, if provides me with the following errors:
error: no matching function for call to 'isBigger'
note: candidate function not viable: requires 2 arguments, but 0 were provided
I'm pretty sure its because of the way i've used isBigger function in the countPairs calling. But that's how generic code works no?
Thank you very much.
Aucun commentaire:
Enregistrer un commentaire