for_each.cpp
来自「For_each函数的用法!」· C++ 代码 · 共 38 行
CPP
38 行
#include <functional>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
//template <class T>
class out_times_x
{
private:
int multiplier;
public:
out_times_x(const int& k) : multiplier(k) { }
void operator()(const int& x) { cout << x * multiplier << " " << endl; }
};
int main ()
{
int sequence[5] = {1,2,3,4,5};
vector<int> v(sequence, sequence+5);
out_times_x f2(3);
for_each(v.begin(),v.end(),f2); // Apply function
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?