fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <tuple>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. class Point
  11. {
  12. public:
  13. Point(double x, double y, double z):_x(x),_y(y),_z(z) {}
  14. double x(void) const { return _x; }
  15. double y(void) const { return _y; }
  16. double z(void) const { return _z; }
  17.  
  18. double& x(void) { return _x; }
  19. double& y(void) { return _y; }
  20. double& z(void) { return _z; }
  21.  
  22. double _x,_y,_z;
  23. };
  24.  
  25. struct Xitem
  26. {
  27. int ID;
  28. Point eP;
  29. };
  30.  
  31.  
  32. class X_Struct_Sorter
  33. {
  34. public:
  35. X_Struct_Sorter(){}
  36. bool operator()(Xitem a,Xitem b)
  37. {
  38. return std::tie(a.eP.x(),a.eP.y(),a.eP.z(),a.ID)
  39. <std::tie(b.eP.x(),b.eP.y(),b.eP.z(),b.ID);
  40. }
  41.  
  42. };
  43.  
  44. int main() {
  45. vector<Xitem> items = { { 0, Point(1,0,0) }, { 1, Point(0,0,0) }, { 2, Point(0,-1,0) } };
  46.  
  47. for (auto x : items)
  48. {
  49. std::cout << x.eP.x() << ", " << x.eP.y() << ", " << x.eP.z() << ", " << x.ID << std::endl;
  50. }
  51. std::cout << "Sorted:" << std::endl;
  52. std::sort(items.begin(), items.end(), X_Struct_Sorter());
  53.  
  54. for (auto x : items)
  55. {
  56. std::cout << x.eP.x() << ", " << x.eP.y() << ", " << x.eP.z() << ", " << x.ID << std::endl;
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
1, 0, 0, 0
0, 0, 0, 1
0, -1, 0, 2
Sorted:
0, -1, 0, 2
0, 0, 0, 1
1, 0, 0, 0