功能描述:
- 实现容器的遍历
函数原型:
- for_each(iterator beg, iterator end, _func);
//遍历算法 遍历容器元素
//beg开始迭代器
//end结束迭代器
//_func函数或者函数对象
代码演示:
#include<iostream>using namespace std;#include<algorithm>#include<vector>//常用遍历算法 for_each//普通函数void print01(int val) {cout << val << " ";}//仿函数class print02 {public:void operator()(int val) {cout << val << " ";}};void test01() {vector<int>a;for (int i = 0; i < 10; i++) {a.push_back(i);}//普通函数放的是函数名for_each(a.begin(), a.end(), print01);cout << endl;//仿函数放的是函数对象for_each(a.begin(), a.end(), print02());cout << endl;}int main() {test01();return 0;}
运行结果:
