fork(5) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. int swapCount = 0;
  6.  
  7. class X
  8. {
  9. public:
  10. int x;
  11.  
  12. X( int val ): x( val ) {};
  13.  
  14. friend void swap( X &a, X &b )
  15. {
  16. using std::swap;
  17. swap( a.x, b.x );
  18. swapCount++;
  19. }
  20. };
  21.  
  22. int main() {
  23. std::vector<X> vec;
  24. for ( int i = 0; i < 100; i++ )
  25. {
  26. vec.push_back( X( i ) );
  27. };
  28. std::cout << "swapCount=" << swapCount << "\n";
  29.  
  30. std::sort( vec.begin(), vec.end(),
  31. [] ( const X &a, const X &b ) -> bool { return (a.x) < (b.x); } );
  32. std::cout << "swapCount=" << swapCount << "\n";
  33.  
  34. std::sort( vec.begin(), vec.end(),
  35. [] ( const X &a, const X &b ) -> bool { return (a.x) < (b.x); } );
  36. std::cout << "swapCount=" << swapCount << "\n";
  37.  
  38. std::sort( vec.begin(), vec.end(),
  39. [] ( const X &a, const X &b ) -> bool { return (a.x) < (b.x); } );
  40. std::cout << "swapCount=" << swapCount << "\n";
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
swapCount=0
swapCount=13
swapCount=26
swapCount=39