Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > design question - way to specify what attribute of an object to process in a generic function

Reply
Thread Tools

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

 
 
Alan
Guest
Posts: n/a
 
      06-16-2007
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      06-16-2007
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.
 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      06-17-2007
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
 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      06-17-2007
Thank you. This was very helpful. Alan

 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      06-18-2007
On Jun 16, 10:33 pm, Alan <jalantho...@verizon.net> 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

 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      06-18-2007
Never mind. I found some information and discovered on another
thread that I should use "result_type."

Thanks again, Alan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to specify an undetermined Object (Generic) as parameter in method() ? Gianni Galore Java 0 01-18-2011 08:24 PM
xml schema to specify element type based on attribute value? James XML 2 07-28-2007 04:11 AM
simple problem trying to specify a unique attribute with XML schema smachin1000@gmail.com XML 2 07-27-2006 08:07 PM
specify attribute with element data too arcofdescent@gmail.com ASP .Net Web Services 1 02-21-2006 08:02 AM
must specify "encoding" attribute in DTD. Why? Kent Tong XML 4 02-23-2004 01:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57