return type of count in std::algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    return type of count in std::algorithm

    Consider the following piece of code:

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cstddef>

    using namespace std;

    int main()
    {
    vector<stringv;

    v.push_back("ze ro");
    v.push_back("on e");
    v.push_back("tw o");
    v.push_back("th ree");
    v.push_back("te st string");
    v.push_back("an other string");

    iterator_traits < vector<string>: :iterator >::difference_t ype c
    =
    count(v.begin() , v.end(), string("test
    string"));

    cout << c << endl;

    int arr[] = { 0, 1, 2, 3, 0, 1, 2 };

    ptrdiff_t i = count(arr, arr + (sizeof(arr) / sizeof(arr[0])),
    2);

    cout << i << endl;

    return EXIT_SUCCESS;
    }

    In the above code, I had to use two different types for the return
    type of std::count().
    Instead is there a common return type which I can use ?

    Also the first return type
    iterator_traits < vector<string>: :iterator >::difference_t ype
    is lengthy. Is there a shorter form ?

    Kindly clarify.

    Thanks
    V.Subramanian
  • Ian Collins

    #2
    Re: return type of count in std::algorithm

    subramanian100i n@yahoo.com, India wrote:
    Consider the following piece of code:
    >
    In the above code, I had to use two different types for the return
    type of std::count().
    Instead is there a common return type which I can use ?
    >
    See Stroustrup 18.5.3 for an informative discussion of this issue.
    Also the first return type
    iterator_traits < vector<string>: :iterator >::difference_t ype
    is lengthy. Is there a shorter form ?
    >
    As always, there's typedef!

    --
    Ian Collins.

    Comment

    Working...