Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. sort(mMyClassVector.begin(), mMyClassVector.end(),
  2. [](const MyClass & a, const MyClass & b)
  3. {
  4. return a.mProperty > b.mProperty;
  5. });
  6.  
  7. sort(mMyClassVector.begin(), mMyClassVector.end(),
  8. [](const MyClass & a, const MyClass & b) -> bool
  9. {
  10. return a.mProperty > b.mProperty;
  11. });
  12.  
  13. #include<array>
  14. #include<functional>
  15.  
  16. int main()
  17. {
  18. std::array<int, 10> vec = { 1,2,3,4,5,6,7,8,9 };
  19. std::sort(std::begin(vec ), std::end(vec ), [](int a, int b) {return a > b; });
  20. for (auto item : vec)
  21. std::cout << item << " ";
  22.  
  23. return 0;
  24. }
  25.  
  26. #include <algorithm>
  27. #include <vector>
  28. #include <iterator>
  29. #include <iostream>
  30. #include <sstream>
  31.  
  32. struct Foo
  33. {
  34. Foo() : _i(0) {};
  35.  
  36. int _i;
  37.  
  38. friend std::ostream& operator<<(std::ostream& os, const Foo& f)
  39. {
  40. os << f._i;
  41. return os;
  42. };
  43. };
  44.  
  45. typedef std::vector<Foo> VectorT;
  46.  
  47. std::string toString(const VectorT& v)
  48. {
  49. std::stringstream ss;
  50. std::copy(v.begin(), v.end(), std::ostream_iterator<Foo>(ss, ", "));
  51. return ss.str();
  52. };
  53.  
  54. int main()
  55. {
  56.  
  57. VectorT v(10);
  58. std::for_each(v.begin(), v.end(),
  59. [](Foo& f)
  60. {
  61. f._i = rand() % 100;
  62. });
  63.  
  64. std::cout << "before sort: " << toString(v) << "n";
  65.  
  66. sort(v.begin(), v.end(),
  67. [](const Foo& a, const Foo& b)
  68. {
  69. return a._i > b._i;
  70. });
  71.  
  72. std::cout << "after sort: " << toString(v) << "n";
  73. return 1;
  74. };
  75.  
  76. before sort: 83, 86, 77, 15, 93, 35, 86, 92, 49, 21,
  77. after sort: 93, 92, 86, 86, 83, 77, 49, 35, 21, 15,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement