3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Duoas
0
template<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.

0

how do you do it with multimaps?

template<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.

0

Argh.

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;
    }
  }
0
template<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.

This is wrong, by the way. "m" looks up "i" as a key in the map. So it will not only fail to compile when "T" is not "int"; but even when it is an int, the binding might not exist.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.