C ++:在w.r.t.类的向量上选择argmax。任意表达 [英] C++: select argmax over vector of classes w.r.t. arbitrary expression

查看:184
本文介绍了C ++:在w.r.t.类的向量上选择argmax。任意表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法描述我的问题,所以我举一个例子:



我有一个类描述,它有一些变量,例如: / p>

  class A {
float a,b,c,d;
}



现在,我维护一个向量< A& / code>,其中包含许多这些类。我需要做的非常经常是找到这个向量内的对象满足其中的一个参数是最大的w.r.t给其他人。即代码看起来像:

  int maxi = -1; 
float maxa = -1000;
for(int i = 0; i res = vec [i] .a;
if(res> maxa){
maxa = res;
maxi = i;
}
}
return vec [maxi];但是,有时候我需要找到最大的类
$ b $ c>,有时最大值 b ,有时最大值 0.8 * a + 0.2 * b 需要最大 a * VAR + b ,其中 VAR 是前面分配的某个变量等。字,我需要评估每个类的表达式,并采取 max 。我发现自己复制粘贴到任何地方,只改变定义 res 的单行。



好的方式来避免这种疯狂的C ++?



谢谢!

解决方案

  template< typename F> 
struct CompareBy
{
bool operator()(const typename F :: argument_type& x,
const typename F :: argument_type& y)
{return f x) f(y); }

CompareBy(const F& f):f(f){}

private:
F f;
};


template< typename T,typename U>
struct Member:std :: unary_function< U,T>
{
成员(T U :: * ptr):ptr(ptr){}
const T& operator()(const U& x){return x。* ptr; }

private:
T U :: * ptr;
};

template< typename F>
CompareBy< F> by(const F& f){return CompareBy(F); }

template< typename T,typename U>
会员< T,U> mem_ptr(T U :: * ptr){return Member< T,U>(ptr); }

您必须包含< functional> 为了这个工作。现在使用< algorithm>

  std :: max_element (v.begin(),v.end(),by(mem_ptr(& A :: a))); 

 双重组合(A x){return 0.2 * xa + 0.8 * xb; } 

  std :: max_element(v.begin(),v.end(),by(std :: fun_ptr(combination))); 

或甚至

 code> struct combination:std :: unary_function< A,double> 
{
组合(double x,double y):x(x),y​​(y){}
double运算符(const A& u){return x * ua + y * ub; }

private:
double x,y;
};

  std :: max_element(v.begin(),v.end(),by(combination(0.2,0.8)));成员或线性成员比较  

成员 a b 的组合。我把比较器分成两个,因为 mem_ptr 事情是有用的,值得重用。 std :: max_element 的返回值是一个迭代器到最大值。你可以取消引用它以获取max元素,也可以使用 std :: distance(v.begin(),i)找到相应的索引$ c>< iterator> 第一个)。



请参阅 http://codepad.org/XQTx0vql 查看完整代码。


I have trouble describing my problem so I'll give an example:

I have a class description that has a couple of variables in it, for example:

class A{
  float a, b, c, d;
}

Now, I maintain a vector<A> that contains many of these classes. What I need to do very very often is to find the object inside this vector that satisfies that one of it's parameters is maximal w.r.t to the others. i.e code looks something like:

int maxi=-1;
float maxa=-1000;
for(int i=0;i<vec.size();i++){
  res= vec[i].a;
  if(res > maxa) {
    maxa= res;
    maxi=i;
  }
}
return vec[maxi];

However, sometimes I need to find class with maximal a, sometimes with maximal b, sometimes the class with maximal 0.8*a + 0.2*b, sometimes I want a maximal a*VAR + b, where VAR is some variable that is assigned in front, etc. In other words, I need to evaluate an expression for every class, and take the max. I find myself copy-pasting this everywhere, and only changing the single line that defines res.

Is there some nice way to avoid this insanity in C++? What's the neatest way to handle this?

Thank you!

解决方案

template <typename F>
struct CompareBy
{
    bool operator()(const typename F::argument_type& x,
                    const typename F::argument_type& y)
    { return f(x) < f(y); }

    CompareBy(const F& f) : f(f) {}

 private:
    F f;
};


template <typename T, typename U>
struct Member : std::unary_function<U, T>
{
    Member(T U::*ptr) : ptr(ptr) {}
    const T& operator()(const U& x) { return x.*ptr; }

private:
    T U::*ptr;
};

template <typename F>
CompareBy<F> by(const F& f) { return CompareBy<F>(f); }

template <typename T, typename U>
Member<T, U> mem_ptr(T U::*ptr) { return Member<T, U>(ptr); }

You need to include <functional> for this to work. Now use, from header <algorithm>

std::max_element(v.begin(), v.end(), by(mem_ptr(&A::a)));

or

double combination(A x) { return 0.2 * x.a + 0.8 * x.b; }

and

std::max_element(v.begin(), v.end(), by(std::fun_ptr(combination)));

or even

struct combination : std::unary_function<A, double>
{
    combination(double x, double y) : x(x), y(y) {}
    double operator()(const A& u) { return x * u.a + y * u.b; }

private:
    double x, y;
};

with

std::max_element(v.begin(), v.end(), by(combination(0.2, 0.8)));

to compare by a member or by linear combinations of a and b members. I split the comparer in two because the mem_ptr thing is damn useful and worth being reused. The return value of std::max_element is an iterator to the maximum value. You can dereference it to get the max element, or you can use std::distance(v.begin(), i) to find the corresponding index (include <iterator> first).

See http://codepad.org/XQTx0vql for the complete code.

这篇关于C ++:在w.r.t.类的向量上选择argmax。任意表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆