for_each()使用函数对象

来源:互联网 发布:淘宝自定义页面优惠券 编辑:程序博客网 时间:2024/06/11 19:46

#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdlib>
#include <iterator>
using namespace std;

class A
{
public:
 A(int i){
 }
 void operator()(int e){
  cout<<e;
 }
};
int mrand()
{
 return rand()%10;
}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> ve;
 generate_n(back_inserter(ve),10,mrand);
 for_each(ve.begin(),ve.end(),A(1)); //A(1)就是通过class创建临时对象
 system("pause");
 return 0;
}

原创粉丝点击