CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2015
    Posts
    175

    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?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  5. #5
    Join Date
    Jun 2015
    Posts
    175

    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.

  6. #6
    Join Date
    Jun 2015
    Posts
    175

    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?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to implement the count function of algorithm ourselves

    Code:
    return i;
    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)

  8. #8
    Join Date
    Jun 2015
    Posts
    175

    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).

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to implement the count function of algorithm ourselves

    Quote Originally Posted by tomy12 View Post
    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)

  10. #10
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: How to implement the count function of algorithm ourselves

    I guess, it should be of an integer value or either an iterator.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  12. #12
    Join Date
    Jun 2015
    Posts
    175

    Re: How to implement the count function of algorithm ourselves

    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured