tags:

views:

168

answers:

1

With reference to this question StackoVerflow 529831, this was one of the suggested approaches

template<typename Map> typename Map::const_iterator 
greatest_less(Map const& m, typename Map::key_type const& k) {
    //How to print K and Map m
    typename Map::const_iterator it = m.lower_bound(k);
    if(it != m.begin()) {
      return --it;
    }
    return m.end();
}

I would be interested in printing key K and Map m, How would go about this.

+3  A: 

Use the << operator, making sure that << is defined for both your Map::key_type and Map::data_type types (you will know if that is not the case as the code will not compile.)

cout << k << endl;
for (typename Map::const_iterator it = m.begin(); it != m.end(); ++i) {
  cout << it->first << " -> " << it->second << endl;
}

e.g. if your Map::data_type is a struct fraction with members float numerator and float denominator,

ostream& operator<<(ostream& os, const fraction& obj) {
   return os << obj.numerator << '/' << obj.denominator;
}

Cheers, V.

vladr
+1 for letting the compiler tell you if you need to write operator<<.
Kristo