How do I get the first 5 elements of a map?
toolbox03
-3
Light Poster
Recommended Answers
Jump to Posttemplate<typename T, typename U> void print_first_five( map<T,U> m ) { for (int i = 0; i < (m.size() >= 5) ? 5 : m.size(); i++) cout << m[i].second; }
Hope this helps.
Jump to PostArgh.
Use an iterator.
template<typename A, typename B> void print_5( const multimap<A, B>& m ) { multimap<A, B>::const_iterator i; for ( int n = 0, i = m.begin(); (n < 5) and (i != m.end()); n++, i++ ) { cout << i->second << endl; } }
All 5 Replies
Duoas
1,025
Postaholic
Featured Poster
toolbox03
-3
Light Poster
Duoas
1,025
Postaholic
Featured Poster
bugmenot
25
Posting Whiz in Training
Duoas
1,025
Postaholic
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.