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