#include <std_lib_facilities_4.h>
usingnamespace std;
template<typename InputIterator, typename Type>
typename iterator_trains<InputIterator>::difference_type count(
InputIterator first, InputIterator last, const Type& val)
{
int i = 0;
for (auto it = first; it != last; ++it)
if (*it == val)
++i;
return i;
}
int main()
{
vector<int> v1 = { 10, 20, 30, 10, 40, 10 };
cout << "v1 = (";
for (constauto v : v1)
cout << v << ' ';
cout << ")\n";
vector<int>::iterator::difference_type result;
result = count(v1.begin(), v1.end(), 10);
cout << "The number of 10s in v1 = " << result << endl;
system("pause");
return 0;
}
I get 14 errors using VS 2017 compiler and it seems all of them refer to the count function used in the code. It's not known for the compiler although I've declared and implemented it above!
Why please?
What does typename iterator_trains<InputIterator>::difference_type mean, too, please?
template<typename InputIterator, typename Type>
typename iterator_traits<InputIterator>::difference_type my_count(
InputIterator first, InputIterator last, const Type& val)
{
// int i = 0;
// algorithms are implemented in terms of iterators
// here we need a way to figure out what would be the right integral type to
// use for counting the number of elements in the sequence being iterated over
// for example, the number of elements between first and last may be larger
// than the largest value that can be held in a variable of type int.
typename iterator_traits<InputIterator>::difference_type i = 0 ;for (auto it = first; it != last; ++it) if (*it == val) ++i;
return i;
}