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 5 1371
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.
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<objectdata;
.... 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
Thank you. This was very helpful. Alan
On Jun 16, 10:33 pm, Alan <jalantho...@verizon.netwrote:
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
Never mind. I found some information and discovered on another
thread that I should use "result_type."
Thanks again, Alan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: grz02 |
last post by:
Hi,
Im an experienced database+software designer and developer,
but, unfortunately, anything to do with web-programming and
web-systems designs is still a pretty new area to me...
(been working...
|
by: Danimal |
last post by:
I have been using PHP for a long time... since it was called PHP/FI. I
have a programming design question:
Let say I have this class:
class attrib {
var $lenght;
var $type;
...
}
|
by: Patchwork |
last post by:
Hi Everyone,
I have a design related question (in C++) that I am hoping someone can help
me with. It is related to my previous post but since it was pointed out that
I was more or less asking...
|
by: Anders |
last post by:
We need to create a lookup structure that can contain a flexible amount of
attributes.
We have discussed different design solutions for lookup-functionality, and
come up with this suggestion:
...
|
by: Charles Law |
last post by:
This is going to seem like a basic OO question, but it comes up and bites me
every now and again.
Suppose we have a multi-tiered protocol to implement, what is the logical,
OO way to design the...
|
by: Jack Addington |
last post by:
I've got a fairly simple application implementation that over time is going
to get a lot bigger. I'm really trying to implement it in a way that will
facilitate the growth. I am first writing a...
|
by: Gregory Guthrie |
last post by:
I am comparing Python to a few other scripting languages, and used a simple
anagrams program as a sample.
I was surprised ast a few python features that did not work as I would
expect/wish;...
|
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= |
last post by:
Hi all
I have some problems with Crystal Reports (Version 10.2, Runtime 2.0).
In Section3 I have added a OLE Object (Bitmap).
Now when I open the report in my code I would
like to set this...
|
by: Anders Borum |
last post by:
Hello,
I've worked on an API for quite some time and have (on several occasions)
tried to introduce generics at the core abstract level of business objects
(especially a hierarchical node). The...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |