#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//普通函数
void print1(int val)
{
cout << val << " ";
}
//函数对象
class print2
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//1.使用函数进行遍历
for_each(v.begin(), v.end(), print1);
cout << endl;
//2.使用仿函数进行遍历
for_each(v.begin(), v.end(), print2());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
01_常用遍历算法_for_each
最新推荐文章于 2024-07-10 22:49:54 发布