You could either do it by a handcrafted for-loop or using STL std::min_element
using a lambda. The way you have used it above is incorrect. Here is the correct way:
auto v = std::vector<std::pair<int,int>>{{0,8},{0,3},{1,4},{2,9},{3,5}};
auto result = *std::min_element(v.cbegin(), v.cend(), [](const auto& lhs, const auto& rhs) {
return lhs.second < rhs.second;
});
std::cout << result.first << " " << result.second << std::endl; // 0 3
You can try this online here and maybe tinker to understand how the lambda works.
std::min_element
will work correctly for native types the way you have used it, but for derived ones like std::pair
with the custom condition of only picking the second, you need to supply the criteria via a lambda.
I'd recommend reading up on C++ lambdas - theyre extremely handy in general but especially when needing to supply a custom predicate like in your case! Here is another example: C++ priority queue in ascending order by specific method for objects