-
November 30th, 2017, 12:01 AM
#1
How to implement the count function of algorithm 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:
Code:
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.
Code:
#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>:ifference_type mean, too, please?
-
November 30th, 2017, 02:45 AM
#2
Re: How to implement the count function of algorithm ourselves
Could you show the exact error messages together with the related code lines?
Victor Nijegorodov
-
November 30th, 2017, 04:06 AM
#3
Re: How to implement the count function of algorithm ourselves
Code:
#include <std_lib_facilities_4.h>
What does this include contain?
Code:
typename iterator_trains<InputIterator>::difference_type count(
I didn't know that c++ involved transport?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 30th, 2017, 09:59 AM
#4
Re: How to implement the count function of algorithm ourselves
Code:
int i = 0;
...
return i;
What is the return type of count() ?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 1st, 2017, 02:55 AM
#5
Re: How to implement the count function of algorithm ourselves
trains was a mistake in place of the right choice traits!
It's the content of that header file.
Now the errors are different:
1- Severity Code Description Project File Line Suppression State
Error (active) E0308 more than one instance of overloaded function "count" matches the argument list: 23
2- Severity Code Description Project File Line Suppression State
Error C2668 'count': ambiguous call to overloaded function 23
The return type of count is: typename iterator_traits<InputIterator>:ifference_type. This is totally unclear for me and I don't think if it can convert to an integer correctly.
-
December 1st, 2017, 03:11 AM
#6
Re: How to implement the count function of algorithm ourselves
I changed the whole code to this:
Code:
#include <C:\Users\Abbasi\Desktop\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;
}
And it's working.
Is it now a correct answer to the exercise?
-
December 1st, 2017, 03:38 AM
#7
Re: How to implement the count function of algorithm ourselves
What is the return type of the function?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 2nd, 2017, 12:29 AM
#8
Re: How to implement the count function of algorithm ourselves
The return type of the function is something vague for me, typename iterator_traits<InputIterator> :: difference_type, but what I've returned is of integer (i).
-
December 2nd, 2017, 04:27 AM
#9
Re: How to implement the count function of algorithm ourselves
Originally Posted by tomy12
The return type of the function is something vague for me, typename iterator_traits<InputIterator> :: difference_type, but what I've returned is of integer (i).
Yes. So what should the return type be?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 2nd, 2017, 06:51 AM
#10
Re: How to implement the count function of algorithm ourselves
I guess, it should be of an integer value or either an iterator.
-
December 2nd, 2017, 09:43 AM
#11
Re: How to implement the count function of algorithm ourselves
As the function return type is
Code:
typename iterator_traits<InputIterator>::difference_type
then that is what the type of i should be to avoid any implicit conversion. Hence
Code:
template<typename InputIterator, typename Type>
typename iterator_traits<InputIterator>::difference_type my_count(
InputIterator first, InputIterator last, const Type& val)
{
typename iterator_traits<InputIterator>::difference_type i = 0;
for (auto it = first; it != last; ++it)
if (*it == val)
++i;
return i;
}
However, if you look at the base type of :: difference_type, on VS2017 this is an int. So in this case it doesn't matter. But in general, the type of the return variable should be the type of the function return.
Last edited by 2kaud; December 2nd, 2017 at 10:08 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 3rd, 2017, 02:01 AM
#12
Re: How to implement the count function of algorithm ourselves
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|