fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. template <class T>
  5. struct S
  6. {
  7. T I;
  8. S()
  9. {
  10. static int i;
  11. int sign = i % 2 ? 1 : -1;
  12. I = ++i * sign;
  13. }
  14.  
  15. bool operator < (const S& rhs) const
  16. {
  17. return I < rhs.I;
  18. }
  19. };
  20.  
  21. namespace std {
  22. template <class T>
  23. inline void swap(S<T>& lhs, S<T>& rhs)
  24. {
  25. std::cout << "called" << std::endl;
  26. int tmp = lhs.I;
  27. lhs.I = rhs.I;
  28. rhs.I = tmp;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. S<int> s[10];
  35. for (unsigned i = 0; i < sizeof(s) / sizeof(s[0]); ++i)
  36. {
  37. std::cout << s[i].I << ' ';
  38. }
  39. std::cout << std::endl;
  40.  
  41. std::sort(s, s + sizeof(s) / sizeof(s[0]));
  42. for (unsigned i = 0; i < sizeof(s) / sizeof(s[0]); ++i)
  43. {
  44. std::cout << s[i].I << ' ';
  45. }
  46. std::cout << std::endl;
  47. }
  48.  
  49.  
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
-1 2 -3 4 -5 6 -7 8 -9 10 
-9 -7 -5 -3 -1 2 4 6 8 10