We know that for vector<int> A, we can use *max_element(A.begin(), A.end()) to find the maximum value in A. However, I would like to know whether there is clean way to find the maximum value in a vector<vector<int>> B, avoiding using for loop?
If we use a for loop, the code could be trivial like:
int maxvalue = INT_MIN;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
maxvalue = max(maxvalue, B[i][j]);
or
int maxvalue = INT_MIN;
for (int i = 0; i < m; i++)
{
int temp = *max_element(B[i].begin(), B[i].end());
maxvalue = max(maxvalue, temp);
}
But I still feel it not clean enough. And I don't like the for loop.
Finally, I chose the following code to do it:
auto itv = max_element(A.begin(), A.end(), [](vector<int>& a, vector<int>& b)
{ return *max_element(a.begin(), a.end()) < *max_element(b.begin(), b.end()); });
int ans = *max_element(itv->begin(), itv->end());
*max_element()of twovector<int>s ?