0

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());
6
  • For loops are great. Combine them with iterators and you are gold. Commented Mar 25, 2016 at 21:25
  • @c-wang: Have you tried using a custom comparator function which compares between *max_element() of two vector<int>s ? Commented Mar 25, 2016 at 21:26
  • No, how? Please give me some hint of the code. Commented Mar 25, 2016 at 21:29
  • @c-wang: I haven't tested it. I am not even sure if something like that would work. Just a passing thought. Commented Mar 25, 2016 at 21:31
  • Loops are fine. I wouldn't worry about using a more complicated method. Commented Mar 25, 2016 at 21:38

4 Answers 4

5
auto max_value = std::accumulate(std::begin(B), std::end(B),
      std::numeric_limits<int>::min(), 
      [] (int cur_max, auto && vec) 
      { 
        return std::max(cur_max, *std::max_element(std::begin(v), std::end(v));
      });
Sign up to request clarification or add additional context in comments.

Comments

0

I used a custom comparison operator in max_element() to get the desired effect. No loops whatsoever, except the implied accumulation that max_element() runs.

bool mycomp(vector<int> a, vector<int> b) {
  return *max_element(a.begin(), a.end()) < *max_element(b.begin(), b.end());
}

vector<vector<int>> vv; // our vector of vectors

auto itv = max_element(vv.begin(), vv.end(), mycomp); // find the vector 
                                                     // with the max element

int answer = *max_element((*itv).begin(), (*itv).end()); // finds the max element 
                                                        // in the desired vector

This is by no means clean. But it does what it says it does.

6 Comments

is it possible to use lambda function to replace mycomp?
With reference to the comment: stackoverflow.com/questions/36228231/…
@c-wang: I see no reason why not.
Thank you for accepting the answer. It is worthy to note that @crazy-eddie code does the same thing, essentially (in perhaps a more efficient way).
This version makes WAY more comparisons than mine.
|
0

Instead of the for loop you could use std::for_each. Maybe something like:

int maxvalue = std::numeric_limits<int>::min();
std::for_each(std::begin(B), std::end(B), [&maxvalue](const auto& v)
{
    maxvalue = std::max(maxvalue, *std::max_element(std::begin(v), std::end(b)));
});

Comments

0

If the thing you want to avoid using loops are long structures in your program, using C++11 you can find the maximum in a single line with loops:

std::vector< std::vector<int> > w;


int max = 0;
for (auto i : w) for (auto j : i) max = j > max ? j : max;

Or

int max = 0;
for (auto i : w) for (auto j : i) if (j > max) max = j;

Anyway, I don't think this is a good practice. This option would be better:

int max = 0;
for (auto i : w)
    for (auto j : i)
        max = j > max ? j : max;

4 Comments

"without for loop"
True, it was just something to make it cleaner, I will update the answer
Worth nothing that putting things in a single line was possible even prior to C++11.
The point is using those smaller for loops. Of course you can even write all your c++ code in one single line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.