I was wondering if there is a succinct way of finding the max value of one of the elements in a vector of tuples. e.g. for the following, say I want to find the largest second value of the tuple in the vector of tuples.
vector<tuple<int, int>> foo = { {12,1},{12,5},{5,6} };
The result should be 6.
One way I could do this would be something like:
vector<double> allFoo;
for (int i = 0; i != size(foo); i++) {
allFoo.emplace_back(get<1>(foo[i]));
}
double maxVal = *max_element(allFoo.begin(), allFoo.end());
I feel though, that because you are essentially iterating over things twice, this could be done much more simply?
My tuple skills are a bit limited and it seems like you should be able to do some kind of max_element directly on foo...