2

I have a vector<int> v{1, 2, 3}

I have a function: int square(int x){return x*x;}

My question is: How do properly I find the argmax (in v) of square(v) ?

In this case 9 is bigger than 4 and 1. Hence result should give 3.

I could play with indices of "square(v)" and then use that index to retrieve the element in v. But maybe there is a c++ built-in function or a more elegant way to proceed?

  • 1
    Thanks @Mat. I did not find it before posting. It solved my problem. Just for information, here is one solution adapted from this answer: `std::vector v = {1, 2, 3};` `auto argMax = std::max_element(v.begin(), v.end(), [](int a, int b){return a*a < b*b;});` `std::cout << "argmax (square(v)) = " << *argMax << std::endl;` – chauvinSimon Jan 23 '19 at 08:44

0 Answers0