nhaa123
nhaa123

Reputation: 9798

Is there a handy way of finding largest element in container using STL?

Is there a way finding largest container inside a container using STL? ATM, I have this rather naïve way of doing it:


int main()
{
        std::vector<std::vector<int> > v;

        ...

        unsigned int h = 0;

        for (std::vector<std::vector<int> >::iterator i = v.begin(); i != v.end(); ++i) {
                if (*i.size() > h) {
                        h = *i.size();
                }
        }
}

Upvotes: 1

Views: 311

Answers (3)

Greg Rogers
Greg Rogers

Reputation: 36429

You can always use std::max_element and pass a custom comparator that compares the size of two std::vector<int> as arguments.

Upvotes: 17

dicroce
dicroce

Reputation: 46770

You could use quick select, and then select the value on the extreme end:

Quick Select

Upvotes: 0

Howard May
Howard May

Reputation: 6649

Have you considered sorting the container using the STL sort methods?

Upvotes: 0

Related Questions