template for an array and a vector

Discussion in 'C++' started by utab, Jul 23, 2010.

  1. utab

    utab Guest

    Dear all,

    How can I make a template function to work both with an array and a
    vector such as:

    template <class T>
    T median(vector<T> v)
    {
    typedef typename std::vector<T>::size_type vec_sz;
    vec_sz size = v.size();
    if (size == 0)
    throw domain_error("median of an empty vector");
    sort(v.begin(), v.end());
    vec_sz mid = size/2;
    return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];
    }

    Best,
    Umut
     
    utab, Jul 23, 2010
    #1
    1. Advertising

  2. utab

    utab Guest

    On Jul 24, 12:56 am, Victor Bazarov <> wrote:
    > On 7/23/2010 6:47 PM, utab wrote:
    >
    > > How can I make a template function to work both with an array and a
    > > vector such as:

    >
    > > template<class T>
    > > T median(vector<T>  v)
    > > {
    > > typedef typename std::vector<T>::size_type vec_sz;
    > > vec_sz size = v.size();
    > >    if (size == 0)
    > >      throw domain_error("median of an empty vector");
    > >    sort(v.begin(), v.end());
    > >    vec_sz mid = size/2;
    > >    return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];
    > > }

    >
    > I would probably change your 'median' to accept a pair of
    > const_iterators (start and end).  Then you can initialize a local vector
    > with them and do exactly like you do with your 'v' argument.  When you
    > call it for a vector, you do
    >
    >      median(myvector.begin(), myvector.end())
    >
    > and for an array you do
    >
    >      median(array, array + size_of_the_array);
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks Victor,

    the question was not that clear to me whether I could use function
    overloading or not, but that seems as the only opportunity...
    I also thought using some kind of size argument with overloaded
    functions but your solution is better.

    Best,
    Umut
     
    utab, Jul 24, 2010
    #2
    1. Advertising

  3. utab <> writes:

    > On Jul 24, 12:56 am, Victor Bazarov <> wrote:
    >> On 7/23/2010 6:47 PM, utab wrote:
    >>
    >> > How can I make a template function to work both with an array and a
    >> > vector such as:

    >>
    >> > template<class T>
    >> > T median(vector<T> v)
    >> > {
    >> > typedef typename std::vector<T>::size_type vec_sz;
    >> > vec_sz size = v.size();
    >> > if (size == 0)
    >> > throw domain_error("median of an empty vector");
    >> > sort(v.begin(), v.end());
    >> > vec_sz mid = size/2;
    >> > return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];
    >> > }

    >>
    >> I would probably change your 'median' to accept a pair of
    >> const_iterators (start and end). Then you can initialize a local vector
    >> with them and do exactly like you do with your 'v' argument. When you
    >> call it for a vector, you do
    >>
    >> median(myvector.begin(), myvector.end())
    >>
    >> and for an array you do
    >>
    >> median(array, array + size_of_the_array);
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask

    >
    > Thanks Victor,
    >
    > the question was not that clear to me whether I could use function
    > overloading or not, but that seems as the only opportunity...
    > I also thought using some kind of size argument with overloaded
    > functions but your solution is better.


    I would agree that something along the lines of Victor's solution is the
    better way to go. The following is just to give an example of how you
    /could/ go down the function overloading route:

    #include <vector>
    #include <algorithm>
    #include <stdexcept>
    #include <iostream>

    template<class T>
    T median(std::vector<T> v)
    {
    typedef typename std::vector<T>::size_type vec_sz;
    vec_sz size = v.size();
    if (size == 0)
    throw std::domain_error("median of an empty vector");
    std::sort(v.begin(), v.end());
    vec_sz mid = size/2;
    return size % 2 == 0 ? (v[mid] + v[mid - 1])/2 : v[mid];
    }

    template<class T, unsigned N>
    T median(const T(&a)[N])
    {
    T tmp_arr[N];
    for (unsigned i = 0; i < N; ++i)
    tmp_arr = a;
    std::sort(tmp_arr, tmp_arr + N);
    unsigned mid = N/2;
    return N % 2 == 0 ? (tmp_arr[mid] + tmp_arr[mid - 1])/2
    : tmp_arr[mid];
    }

    int main()
    {
    std::vector<double> d_vec;
    const unsigned ARR_SIZE = 6;
    double d_arr[ARR_SIZE] = { 7.9, 2.3, -1.1, 4.7, 9., 4.3 };
    for (unsigned i = 0; i < ARR_SIZE; ++i)
    d_vec.push_back(d_arr);
    std::cout << "median(d_vec): " << median(d_vec) << '\n';
    std::cout << "median(d_arr): " << median(d_arr) << '\n';
    }

    /**
    * Output:
    * median(d_vec): 4.5
    * median(d_arr): 4.5
    */

    There are, perhaps, some design issues here, however. For example, the
    median of a vector<int> or int[] may not necessarily be representable by
    an int, as you are of course aware.

    Regards

    Paul Bibbings
     
    Paul Bibbings, Jul 24, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,938
  2. Replies:
    8
    Views:
    1,961
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  4. Thomas
    Replies:
    7
    Views:
    328
    Mark Hubbart
    May 23, 2005
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    375
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page