design question - way to specify what attribute of an object to process in a generic function

Discussion in 'C++' started by Alan, Jun 16, 2007.

  1. Alan

    Alan Guest

    I was wondering if anyone had design advice on this. . . . I am
    doing some mathematical operations looking at different metrics for
    data (in objects) I have captured. The object class has several data
    attributes, say: metric1, metric2, . . . metricN. These are different
    metrics on which I am running statistics.

    There are a number of operations I have to do on these objects for
    each metric separately: sort, group (i.e., group a list of objects
    based on a metric), etc. I understand the very basics of C++ template
    functions, overloading, etc. However, I cannot figure out a way to
    design a generic function to sort by each single attribute. So, I end
    up with functions like: sort_by_metric1, sort_by_metric2, . . .
    sort_by_metricN.

    Is there a way to avoid so many nearly-duplicative functions? For
    example, is there a way in the function call to tell the function what
    metric I want to sort by? This would allow me to use one, generic
    function vice a bunch of nearly-duplicative ones.

    I hope this is clear. If not, please let me know, and I will
    attempt to clarify.

    Thanks, Alan
    Alan, Jun 16, 2007
    #1
    1. Advertising

  2. Re: design question - way to specify what attribute of an objectto process in a generic function

    Alan wrote:
    ....
    > Is there a way to avoid so many nearly-duplicative functions?


    You can use pointer to member as a parameter to your comparison function
    for sorting.

    Look up "pointer to member" in your fav C++ book - if you have further
    questions, post again.

    BTW - pointer to member may be a template parameter OR a value
    parameter. It probably makes little difference.

    Another wat is to code up different "getter" functions that get
    different metrics and use those functions in your sort parameters.
    Gianni Mariani, Jun 17, 2007
    #2
    1. Advertising

  3. Alan wrote:

    > I was wondering if anyone had design advice on this. . . . I am
    > doing some mathematical operations looking at different metrics for
    > data (in objects) I have captured. The object class has several data
    > attributes, say: metric1, metric2, . . . metricN. These are different
    > metrics on which I am running statistics.


    Something like

    #include <vector>

    struct object {
    double metric1, metric2 ... metricN;
    }

    ....
    std::vector<object> data;
    .... insert objects into data.

    > There are a number of operations I have to do on these objects for
    > each metric separately: sort, group (i.e., group a list of objects
    > based on a metric), etc. I understand the very basics of C++ template
    > functions, overloading, etc. However, I cannot figure out a way to
    > design a generic function to sort by each single attribute. So, I end
    > up with functions like: sort_by_metric1, sort_by_metric2, . . .
    > sort_by_metricN.


    #include <algorithm>
    #include <functional>

    class less_metric1 : std::binary_function<object, object, bool>{
    public:
    return_type operator()(const object& a, const object& b) {
    return a.metric1 < b.metric1;
    }
    }

    ....
    std::sort(data.begin(), data.end(), less_metric1());

    > Is there a way to avoid so many nearly-duplicative functions? For
    > example, is there a way in the function call to tell the function what
    > metric I want to sort by? This would allow me to use one, generic
    > function vice a bunch of nearly-duplicative ones.


    This is the purpose of templates.

    --
    rbh
    Robert Bauck Hamar, Jun 17, 2007
    #3
  4. Alan

    Alan Guest

    Thank you. This was very helpful. Alan
    Alan, Jun 17, 2007
    #4
  5. Alan

    Alan Guest

    On Jun 16, 10:33 pm, Alan <> wrote:
    > Thank you. This was very helpful. Alan


    Robert,
    I do not really understand what the line

    return_type operator()(const object& a, const object& b)

    does in your definition of the binary function. Can you explain?

    I think I understand the rest now. Thank you for the suggestion.

    Alan
    Alan, Jun 18, 2007
    #5
  6. Alan

    Alan Guest

    Never mind. I found some information and discovered on another
    thread that I should use "result_type."

    Thanks again, Alan
    Alan, Jun 18, 2007
    #6
    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. Kent Tong
    Replies:
    4
    Views:
    434
    Richard Tobin
    Feb 23, 2004
  2. Gianni Galore
    Replies:
    0
    Views:
    265
    Gianni Galore
    Jan 18, 2011
  3. Marc Aymerich
    Replies:
    0
    Views:
    70
    Marc Aymerich
    Nov 24, 2013
  4. Devin Jeanpierre
    Replies:
    12
    Views:
    105
    Steven D'Aprano
    Nov 24, 2013
  5. Chris Angelico
    Replies:
    0
    Views:
    58
    Chris Angelico
    Nov 24, 2013
Loading...

Share This Page