fork(1) download
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <functional>
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7.  
  8. struct point
  9. {
  10. int x, y, z;
  11. };
  12.  
  13. double distance(point l, point r)
  14. {
  15. int x = l.x - r.x;
  16. int y = l.y - r.y;
  17. int z = l.z - r.z;
  18. return std::sqrt(x*x + y*y + z*z);
  19. }
  20.  
  21. bool orderByDist(point l, point r, point R)
  22. {
  23. return distance(l, R) < distance(r, R);
  24. }
  25.  
  26. int main()
  27. {
  28. //Initial array
  29. point arr[5] = {{1,1,1}, {1,2,3}, {-2, 5, 7}, {0, 5, -3}, {-1, 4, -2}};
  30. //Reference point
  31. point Ref = {1,1,1};
  32.  
  33. using namespace std::placeholders;
  34. auto pred = std::bind(orderByDist, _1, _2, Ref);
  35.  
  36. std::sort(arr, arr + 5, pred);
  37.  
  38. //Output
  39. for(point s: arr)
  40. std::cout << std::setw(3) << s.x <<
  41. std::setw(3) << s.y <<
  42. std::setw(3) << s.z <<
  43. std::setw(6) << std::setprecision(3) << distance(s, Ref) << std::endl;
  44. }
  45.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
  1  1  1     0
  1  2  3  2.24
 -1  4 -2  4.69
  0  5 -3  5.74
 -2  5  7  7.81