fork(2) 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. std::cout << "swap(" << a.x << "," << b.x << ")\n";
  19. swapCount++;
  20. }
  21. };
  22.  
  23. int main() {
  24. std::vector<X> vec;
  25. for ( int i = 0; i < 100; i++ )
  26. {
  27. vec.push_back( X( i ) );
  28. };
  29. std::cout << "swapCount=" << swapCount << "\n";
  30.  
  31. std::sort( vec.begin(), vec.end(),
  32. [] ( const X &a, const X &b ) -> bool { return (a.x) < (b.x); } );
  33. std::cout << "swapCount=" << swapCount << "\n";
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
swapCount=0
swap(50,0)
swap(75,51)
swap(88,76)
swap(52,75)
swap(51,75)
swap(64,53)
swap(1,50)
swap(0,50)
swap(26,2)
swap(39,27)
swap(3,26)
swap(2,26)
swap(15,4)
swapCount=13