473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Read the size of each vector in a map of vectors

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 14 '06 #1
3 2724
co*********@gmail.com 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.
Aug 15 '06 #2

Mark P wrote:
Nothing directly.
Thanks Mark.
This is what I wanted to learn.

Aug 15 '06 #3
In article <11*********************@b28g2000cwb.googlegroups. com>,
co*********@gmail.com 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 ) );
----------------------------------------------------------------------
Aug 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BCC | last post by:
Hi, I have a tab separated value table like this: header1 header2 header3 13.455 55.3 A string 4.55 5.66 Another string I want to load this guy...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: kak3012 | last post by:
Hi, I have a text file I will read it and write out binary. The file includes 256 coloums. I use while (infile.good()) { infile.getline (buffer,2200);
2
by: eb | last post by:
I have this working code : foo.h /* nothing */ foo.cpp ... std::vector<std::vector<T * my_T(board_size,board_size) ; for (i=0; i<board_size; i++)
7
by: ottawajn | last post by:
Dear there, I want to do the follows. (1)initial an array, say myarray={1 2 3.1 4}; (2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0}; (3)let myarray={1,0,2,0,3.1,0,4,0}; (I have not...
24
by: Ground21 | last post by:
Hello. How could I read the whole text file line after line from the end of file? (I want to ~copy~ file) I want to copy file in this way: file 1: a b
4
by: shuisheng | last post by:
Dear All, I have a question. Assume #include <vector> using namespace std; class A { private:
4
by: Edward Jensen | last post by:
Hi, I have the following static arrays of different size in a class: in header: static double w2, x2; static double w3, x3; static double w4, x4; in GaussLegendre.cpp:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.