tags:

views:

130

answers:

3

Dears,

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();
                }
        }
}

A: 

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

Howard May
why would that be a good solution?
anon
A: 

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

Quick Select

dicroce
+16  A: 

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

Greg Rogers
+1 STL algorithms FTW!
ceretullis