#include <algorithm>                                                                                                                                     
#include <iostream>                                                                                                                                      
#include <vector>         
#include <functional>
 
using namespace std;                                                                                                                                     
                                                                                                                                                         
class Foo                                                                                                                                                
{                                                                                                                                                        
  public:                                                                                                                                                
    Foo(void)                                                                                                                                            
    {                                                                                                                                                    
      for(int i = 0; i < 1000; i++)                                                                                                                        
        v.push_back(i);                                                                                                                                  
    }                                                                                                                                                    
                                                                                                                                                         
    void process(function<void(int)> f)                                                                                                                  
    {                                                                                                                                                    
      for(std::vector<int>::iterator i = v.begin(); i != v.end(); i++)                                                                                   
        f(*i);                                                                                                                                           
    }                                                                                                                                                    
                                                                                                                                                         
private:                                                                                                                                                 
  vector<int> v;                                                                                                                                         
};                                                                                                                                                       
                                                                                                                                                         
int main()                                                                                                                                               
{                                                                                                                                                        
  int evenCount = 0;                                                                                                                                     
                                                                                                                                                         
  Foo f;                                                                                                                                                 
                                                                                                                                                         
  f.process(function<void (int)>([&evenCount](int n) {                                                                                                   
    cout << n;                                                                                                                                           
    if(n % 2 == 0) {                                                                                                                                     
      cout << " is even" << endl;                                                                                                                        
      evenCount += 1;                                                                                                                                    
    } else {                                                                                                                                             
      cout << " is odd" << endl;                                                                                                                         
    }                                                                                                                                                    
  }));                                                                                                                                                   
                                                                                                                                                         
  // Print the count of even numbers to the console.                                                                                                     
  cout << "There are " << evenCount << " even numbers in the vector." << endl;                                                                           
                                                                                                                                                         
  return 0;                                                                                                                                              
}                   

Posted by 명혀니
,