#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
//打印数组
class bianli
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void printVector(int val)
{
cout << val <<" ";
}
void test01()
{
vector<int> v1; //此时,v1的大小和容量都为0.
for (int i = 0; i < 10; i++)
{
v1.push_back(i);//向v1中插入数据.
}
//用for_each遍历v1 普通函数输出 后面不带括号
vector<int>::iterator it = v1.begin();
for_each(it,v1.end(),printVector );
cout << endl;
bianli shuchu0;
//用for_each遍历v1 仿函数输出 后面带括号 实际上传的是一个匿名对象.
for_each(it, v1.end(), bianli());
cout << endl;
for_each(it, v1.end(), shuchu0);
}
int main() {
test01();
}
输出结果: