STL Map Use

STL 2 Comments »

What’s wrong with the following code?

  1. template<typename T1, typename T2>
  2. struct my_pair
  3. {
  4.     typedef T1 first_type;
  5.     typedef T2 second_type;
  6.  
  7.     my_pair() : first(T1()), second(T2()) {}
  8.     my_pair(const T1& v1, const T2& v2) : first(v1), second(v2) {}
  9.  
  10.     T1 first;
  11.     T2 second;
  12. };
  13.  
  14. template<typename T1, typename T2>
  15. inline bool operator<
  16.     (
  17.     const my_pair<T1, T2>& x,
  18.     const my_pair<T1, T2>& y
  19.     )
  20. {
  21.     return (x.first < y.first || x.second < y.second);
  22. }
  23.  
  24. void f()
  25. {
  26.     typedef my_pair<…, …> key_type;
  27.     typedefvalue_type;
  28.     typedef std::map<key_type, value_type> map_type;
  29.  
  30.     map_type map;
  31.     // Use map
  32. }

Read the rest of this entry »

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in