I'm having some difficulties with the below generic function, I want this function to take 2 iterators and a value. The function should iterate between the 2 iterators and check for any occurrences of the value and count the occurrences. But I get the following error:
no matching function for call to 'count_c(int, std::vector::iterator, std::vector::iterator)'
template<typename Container>
int count_c(Container i, typename Container::iterator &start, typename Container::iterator &end){
typedef typename Container::iterator Iter;
int count;
for(Iter p = start; p != end; p++){
if((*p) == i){
count = count + 1;
}
return count;
}
int main(){
vector<double> myv;
myv.push_back(9);
myv.push_back(10);
count_c(9, myv.begin(), myv.end());
return 0;
}
It's part of an exam question:
Write a generic function count which:
Takes as parameters a value and two iterators of a container (where to start from and where to finish in the container).
It uses the iterators to go through the elements of the container and count the occur- rences of the value in the container.
Finally, it returns the number of occurrences of the value it has found.
The two iterators are not necessarily the same as what is returned by a con- tainer’s begin() and end() methods!