Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

finding max value of a struct construction inside vector c++

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading