Read the size of each vector in a map of vectors

Discussion in 'C++' started by costantinos@gmail.com, Aug 15, 2006.

  1. Guest

    Hello.

    I have a map
    map<int, vector<int> >
    and I want to find which one of the vectors has the most elements.
    Is there a built in function that does that or do I have to write my
    one code.

    Cheers
    Costantinos
    , Aug 15, 2006
    #1
    1. Advertising

  2. Mark P Guest

    wrote:
    > Hello.
    >
    > I have a map
    > map<int, vector<int> >
    > and I want to find which one of the vectors has the most elements.
    > Is there a built in function that does that or do I have to write my
    > one code.
    >
    > Cheers
    > Costantinos
    >


    Nothing directly. You can use std::max_element if you write a
    comparison function that takes two objects of the map's value_type
    (which, you must remember, is a pair). Probably no less effort than
    just writing a simple loop though.
    Mark P, Aug 15, 2006
    #2
    1. Advertising

  3. Guest

    Mark P wrote:

    > Nothing directly.


    Thanks Mark.
    This is what I wanted to learn.
    , Aug 15, 2006
    #3
  4. Daniel T. Guest

    In article <>,
    wrote:

    > Hello.
    >
    > I have a map
    > map<int, vector<int> >
    > and I want to find which one of the vectors has the most elements.
    > Is there a built in function that does that or do I have to write my
    > one code.


    That depends... (BTW thanks for the interesting exorcise.)

    'max_element' will return the first iterator that satisfies the binary
    predicate, so:
    ----------------------------------------------------------------------
    typedef map< int, vector<int> > IntMap;

    bool comp_sizes( const IntMap::value_type& lhs,
    const IntMap::value_type& rhs ) {
    return lhs.second.size() < rhs.second.size();
    }

    void foo( const IntMap& m ) {
    IntMap::const_iterator it = max_element( m.begin(), m.end(),
    &comp_sizes );
    // 'it' points to the one you want, or m.end() if m was empty.
    }
    ----------------------------------------------------------------------

    If you want the last key, then use rbegin() and rend().

    If however you want all of the keys with the biggest vectors, then:

    ----------------------------------------------------------------------
    template < typename OutIt >
    void copy_keys_with_biggest_vectors( IntMap::const_iterator begin,
    IntMap::const_iterator end, OutIt result ) {
    if ( begin != end ) {
    begin = max_element( begin, end, comp_sizes );
    vector< int >::size_type target = begin->second.size();
    while ( begin != end ) {
    if ( begin->second.size() == target )
    *result++ = begin->first;
    ++begin;
    }
    }
    }
    ----------------------------------------------------------------------

    If you find that you are collecting keys based on some predicate in
    several places in your code then you might want to consider a more
    generic algorithm:

    ----------------------------------------------------------------------
    template < typename FwIt, typename OutIt, typename Pred >
    void copy_keys_if( FwIt begin, FwIt end, OutIt result, Pred pred ) {
    while ( begin != end ) {
    if ( pred( *begin ) )
    *result++ = begin->first;
    ++begin;
    }
    }
    ----------------------------------------------------------------------

    Then you can collect all the keys of vectors that have a particular size
    by:

    ----------------------------------------------------------------------
    struct has_size {
    size_t target;
    has_size( size_t target_ ) : target( target_ ) { }
    bool operator()( const IntMap::value_type& elem ) const {
    return elem.second.size() == target;
    }
    };


    vector< int > keys;
    copy_keys_if( intMap.begin(), intMap.end(), back_inserter( keys ),
    has_size( target ) );
    ----------------------------------------------------------------------
    Daniel T., Aug 15, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Creighton Hogg

    vector of vector of vectors

    Creighton Hogg, Jan 20, 2006, in forum: C++
    Replies:
    4
    Views:
    332
    Daniel T.
    Jan 21, 2006
  2. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  3. easy
    Replies:
    8
    Views:
    390
  4. Replies:
    3
    Views:
    675
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    426
    Guest
    Sep 14, 2005
Loading...

Share This Page