473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2742
co*********@gma il.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_elemen t 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************ *********@b28g2 000cwb.googlegr oups.com>,
co*********@gma il.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_t ype& lhs,
const IntMap::value_t ype& rhs ) {
return lhs.second.size () < rhs.second.size ();
}

void foo( const IntMap& m ) {
IntMap::const_i terator 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_i terator begin,
IntMap::const_i terator 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_t ype& elem ) const {
return elem.second.siz e() == 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
13376
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 into a vector of vectors, since I do not know how long it may be. I think I have to have a vector of vectors of strings, and then extract the doubles later(?):
25
7597
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() might take linear time on some list implementations. That makes sense at first. However, he also says this at the very beginning: "That being the case , you might wonder why one construct should be preferred to the other, especially in view of the...
2
22834
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: operator delete(void*) (vg_replace_malloc.c:156) ==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86) ==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
2
6721
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
3077
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
1568
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 idea how to figure it out)
24
3227
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
1927
by: shuisheng | last post by:
Dear All, I have a question. Assume #include <vector> using namespace std; class A { private:
4
5863
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.