Implementing algorithm::count ourselves

Hi,

Exercise 3 of chapter 21 of the book Programming Principles and Practice Using C++ says:
Implement count yourself and test it.

I sought it and found the declaration of that function:
1
2
3
template<typename InputIterator, typename Type>
typename iterator_trains<InputIterator>::difference_type count(
	InputIterator first, InputIterator last, const Type& val)


And then tried to implement it using the simplest and first way that comes in mind.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <std_lib_facilities_4.h>
using namespace 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 (const auto 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?
Last edited on
What does typename iterator_trains<InputIterator>::difference_type mean, too, please?
It means nothing. The correct spelling is

typename iterator_traits<InputIterator>::difference_type

Note: iterator_traits not trains

See:

http://www.cplusplus.com/reference/iterator/iterator_traits/
http://en.cppreference.com/w/cpp/iterator/iterator_traits
Yeah, it was right.
The definitions are not easily understood and if not that important, I may pass over them focusing more on the code.
I wrote this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <std_lib_facilities_4.h>
using namespace std;

template<typename InputIterator, typename Type>
typename iterator_traits<InputIterator>::difference_type my_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 (const auto v : v1)
		cout << v << ' ';
	cout << ")\n";

	vector<int>::iterator::difference_type result = my_count(v1.begin(), v1.end(), 10);

	cout << "The number of 10s in v1 = " << result << endl;

	system("pause");
	return 0;
}


It works as well.
What's your opinion on this please?
Would you please have a small explanation on the issue?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}
Thank you very much.
Registered users can post here. Sign in or register to post.