enum class comp {
age,
weight
};
struct person {
int age;
int weight;
static auto max(std::vector<person> x, comp(y)) {
if (comp(y) == comp::age) {
for (person e : x) {
auto max = 0;
for (card e : x) {
if (max < e)
max = e;
}
}
}
i am trying to find the max value of age, weight from this vector of a struct construction.
>Solution :
here is a corrected version of your code. As cory post shows you can do this with standard algorithms in a much cleaner way
struct person {
int age;
int weight;
static int max(std::vector<person> x, comp y) {
if (y == comp::age) {
for (person e : x) {
auto max = 0;
for (auto e : x) {
if (max < e.age)
max = e.age;
}
return max;
}
}
return 0;
}
};
int main()
{
person p1;
person p2;
p1.age=42;
p2.age=4;
std::vector<person> pp{p1,p2};
int maxAge = person::max(pp, comp::age);
}