fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. class Foo
  6. {
  7. public:
  8. Foo(std::string&& str_) :
  9. str(std::move(str_))
  10. {}
  11.  
  12. bool operator<(const Foo& other) const
  13. {
  14. return str < other.str;
  15. }
  16.  
  17. std::string getString()
  18. {
  19. return str;
  20. }
  21.  
  22. private:
  23. std::string str;
  24. };
  25.  
  26. int main() {
  27.  
  28. std::vector<Foo> v;
  29. v.push_back(Foo("2"));
  30. v.push_back(Foo("3"));
  31. v.push_back(Foo("1"));
  32.  
  33. std::sort(v.begin(), v.end());
  34. for (auto x : v)
  35. std::cout << x.getString() << std::endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 3236KB
stdin
Standard input is empty
stdout
1
2
3