If I have the following:
#include
#include
#include
#include
struct Features{ int F1, F2,
With a combination of std::transform
, std::multiset
, and an insert iterator you could.
vector<Features> v;
...fill it up
multiset<int> ms;
transform(v.begin(), v.end(), inserter(ms, ms.begin()), criterionFunction);
Then the three max values are the last three elements.
Here is an example using nth_element
with a simpler feature object and criterion function (to reduce clutter):
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
typedef int Features;
int criterionFunction(Features features) {
return features;
}
int main() {
std::vector<Features> v { 0, 4, 2, 5, 4, 3, -2, 1 };
std::nth_element(v.begin(), v.begin() + 3, v.end(),
[](Features a, Features b) {
return criterionFunction(a) > criterionFunction(b);
});
std::copy(v.begin(), v.begin() + 3,
std::ostream_iterator<Features>(std::cout, " "));
}
For your original Features
object, it might be useful to cache/memoize the results of the criterionFunction
to prevent duplicate calls.
Note that nth_element
does not sort the elements in the two partitions; if you want the first three elements in sorted order, use partial_sort
instead.
You can't. It's not what std::transform
does.
transform
applies a single function to every element in the sequence. It does not select specific elements.