Advertisement
andruhovski

FunctorDemo

Apr 12th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. // ConsoleApplication4.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <functional>
  7. #include <algorithm>
  8. #include <iostream>
  9.  
  10. class Animal
  11. {
  12. public:
  13.     Animal();
  14.     ~Animal();
  15.     void live() { age += 5; };
  16.     int getAge(){ return age; };
  17.     int getMaxAge(){ return maxage; };
  18.     bool dead() { return age > maxage; }
  19. private:
  20.     bool health;
  21.     int age, maxage;
  22. };
  23.  
  24. Animal::Animal()
  25. {
  26.     age = 0;
  27.     maxage = 3650;
  28. }
  29.  
  30. Animal::~Animal()
  31. {
  32. }
  33.  
  34. using namespace std;
  35. int _tmain(int argc, _TCHAR* argv[])
  36. {
  37.     std::vector<Animal> zoopark(100);
  38.     for (size_t i = 0; i < 100; i++)
  39.     {
  40.         if (i % 2) {
  41.             for (size_t j = 0; j < 3000; j++)
  42.             {
  43.                 zoopark[i].live();
  44.             }
  45.         }
  46.     }
  47.     cout << count_if(zoopark.begin(), zoopark.end(),
  48.         mem_fun_ref(&Animal::dead));
  49.  
  50.     //std::cout << zoopark[1].getAge() << " " << zoopark[1].getMaxAge() << "\n";
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement