Advertisement
STANAANDREY

sort animals stl

Oct 26th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Animal {
  5. public:
  6.     Animal(string name, int age): name(name), age(age) {}
  7.     inline int getAge() const {
  8.         return this->age;
  9.     }
  10.     friend ostream& operator << (ostream &out, const Animal &animal) noexcept ;
  11. private:
  12.     string name;
  13.     int age;
  14. };
  15.  
  16. ostream& operator << (ostream &out, const Animal &animal) noexcept {
  17.     out << animal.name << ' ' << animal.age;
  18.     return out;
  19. }
  20.  
  21. class Dog : public Animal {
  22. public:
  23.     Dog(string name, int age): Animal(name, age) {}
  24.  
  25. };//*/
  26. class Cat : public Animal {
  27. public:
  28.     Cat(string name, int age): Animal(name, age) {}
  29. };
  30.  
  31. int main() {
  32.     vector<Animal> animals = {
  33.         Dog("azor", 11), Cat("thomas", 5), Dog("rex", 12)
  34.     };
  35.     sort(animals.begin(), animals.end(), [](const auto &fi, const auto &se) {
  36.         return fi.getAge() < se.getAge();
  37.     });
  38.     for (const auto &animal : animals) {
  39.         cout << animal << endl;
  40.     }
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement