fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Vector3
  7. {
  8. Vector3( const int& a, const int& b, const int& c )
  9. {
  10. m_iaData[0] = a;
  11. m_iaData[1] = b;
  12. m_iaData[2] = c;
  13. }
  14.  
  15. int m_iaData[3];
  16. };
  17.  
  18. class Comparsion
  19. {
  20. public:
  21. int idx;
  22. Comparsion(const int& i):idx(i){};
  23. bool operator() ( const Vector3& v1, const Vector3& v2 ) const
  24. {
  25. return v1.m_iaData[idx] < v2.m_iaData[idx];
  26. }
  27. };
  28.  
  29. int main() {
  30. // your code goes here
  31. vector<Vector3> v3;
  32. Vector3 AA(10,0,0);
  33. v3.push_back(AA);
  34. Vector3 BB(20,0,0);
  35. v3.push_back(BB);
  36. Vector3 CC(30,0,0);
  37. v3.push_back(CC);
  38.  
  39. sort(v3.begin(),v3.end(),Comparsion(0));
  40.  
  41. for(vector<Vector3>::iterator it = v3.begin(); it !=v3.end(); ++it)
  42. {
  43. cout << (*it).m_iaData[0] << endl;
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
10
20
30