|
-
July 2nd, 2009, 09:11 AM
#5
Re: math function on individual struct attributes in an array
 Originally Posted by Lindley
The key will be defining predicate functors which operate only on the particular field(s) that interest you.
For instance, you could write a comparison functor which defines a less-than relation based only on data1, and then use it with std::nth_element() to find the median in that sense. Then define another functor which compares based on something else and run the algorithm again.
So long as you can get away with comparison functors, you can stick with the standard library. If you want to get into any of the algorithms in <numeric>, though, you'll probably need to break out boost::transform_iterator and write "selection" functors.
I have comparison functions for that. I use them as the third argument for std::sort(). I am not allowed to use boost.
The algorithms works, I just want a more elegant and generic way of doing it. Right now for each data I need both a comparison function and a median function. But in reality, the data types may be the same type (all ints). So if my struct grows to include dataC, dataD, dataE and dataF, I'll need 12 functions. Can I narrow that down to only needing two functions?
This is tough to describe so bear with me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|