gpt4 book ai didi

c++ - 如何按某些属性对列表进行排序(只有列表的一个条目)C++

转载 作者:行者123 更新时间:2023-11-27 23:38:51 25 4
gpt4 key购买 nike

我有一个代表鱼(鳟鱼)许多特征的列表。这些特征之一是生物体的长度 (L)。我对多条鳟鱼进行了编码,因此最终在我的“鳟鱼”列表中有许多具有相同类型属性的鱼。我想按仅一个属性(长度)的降序对列表进行排序,以便列表的第一个对象是最长的鱼,依此类推。我找不到关于 C++ 的有用帖子,我希望有人能帮助我更快地解决这个问题,谢谢!

我曾尝试将 Trout 列表与只有长度的列表压缩在一起,按长度进行子顺序排序,然后仅选择 Trout 条目,但我没有成功。

C++

class Trout {
public:
string species;
double U_E, V, U_H, U_R;
double e_scaled, L;
double SA, SC;
int age, mom_birth, mom_metamorph, mom_maturation;
double scatter;
int ag_step;
double age_acc, h_acc, Hazard;
bool spawn;
int year_reproduction;
int day_reproduction;
int day_birth;
int year_birth;
double L_j;
double L_b ;
double pA_m_surf, Ub_H, Uj_H, Up_H, g, J_X_Am, E_m_vol, pX_m, L_max;

Trout(string spc, double aa, double bb, double cc, double dd, double ee, double gg, double hh,
int oo,int mb, int mom, int mmat, double ind_variability, double aging_acceleration,
double hazard_rate, double Hazard_f, bool spw, int year_r, int day_r, int day_b, int year_b,
double MaxassRate, double matB, double matJ, double matP,
double EnRatio, double ingRate, double Edensity, double EingRate, double LengthM,
double Lb_sc, double Lj_sc);
virtual ~Trout(){}
void printFriendInfo();
};

Trout::Trout(string spc, double aa, double bb, double cc, double dd, double ee, double gg, double hh,
int oo,int mb, int mom, int mmat, double ind_variability , double aging_acceleration,
double hazard_rate, double Hazard_f, bool spw, int year_r, int day_r, int day_b, int year_b,
double MaxassRate, double matB, double matJ, double matP,
double EnRatio, double ingRate, double Edensity, double EingRate, double LengthM,
double Lb_sc, double Lj_sc)
{
species = spc;
U_E = aa; U_H = bb; U_R = cc;
e_scaled = dd;

L = ee;

SA = gg; SC = hh;
age = oo;
mom_birth = mb; mom_metamorph = mom; mom_maturation = mmat;
scatter = ind_variability;
age_acc = aging_acceleration;
h_acc = hazard_rate;
Hazard = Hazard_f;
spawn = spw;
year_reproduction = year_r;
day_reproduction = day_r;
day_birth = day_b;
year_birth = year_b;
pA_m_surf = MaxassRate;
Ub_H = matB; Uj_H = matJ; Up_H = matP;
g = EnRatio;
J_X_Am = ingRate;
E_m_vol = Edensity;
pX_m = EingRate;
L_max = LengthM;
L_b = Lb_sc;
L_j = Lj_sc;
};

最佳答案

将 std::sort 与自定义比较器一起使用(在我的示例中使用 lambda)。

std::vector<Trout> trouts;
auto cmp = [](const Trout& lhs, const Trout& rhs){ return lhs.L < rhs.L; };
std::sort(trouts.begin(), trouts.end(), cmp);

关于c++ - 如何按某些属性对列表进行排序(只有列表的一个条目)C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57179732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com