fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct WG
  8. {
  9. string name;
  10. int points;
  11. int level;
  12. WG() {}
  13. WG(const string& name, int points, int level)
  14. : name(name), points(points), level(level) {}
  15.  
  16. bool operator<(WG const& o) const {
  17. // zwracasz true jesli obiekt "*this" ma wystepowac przed obiektem "o" po posortowaniu
  18. return name < o.name || (name == o.name && points < o.points);
  19. }
  20. };
  21.  
  22. int main()
  23. {
  24. vector<WG> v;
  25. v.push_back(WG("asd", 123, 0));
  26. v.push_back(WG("xyz", 123, 1));
  27. v.push_back(WG("xyz", 121, 2));
  28. v.push_back(WG("asd", 121, 3));
  29. sort(v.begin(), v.end());
  30. for (size_t i = 0; i < v.size(); i++)
  31. {
  32. cout << "v[" << i << "] = { \""
  33. << v[i].name << "\", "
  34. << v[i].points << ", "
  35. << v[i].level << " }" << endl;
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
v[0] = { "asd", 121, 3 }
v[1] = { "asd", 123, 0 }
v[2] = { "xyz", 121, 2 }
v[3] = { "xyz", 123, 1 }