#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class PrintInt
{
public:
void operator() (int elem) const
{
cout << elem << '\t';
}
};
class PrintInt2
{
public:
PrintInt2(int x)
: m_x(x)
{ }
void operator() (int elem) const
{
cout << elem + m_x << '\t';
}
private:
int m_x;
};
typedef void (*op)(int &elem);
void Func(int &elem)
{
cout << 2*elem << '\t';
}
int main(void)
{
vector<int> coll;
op p = Func;
for (int i=1; i<=9; i++)
{
coll.push_back(i);
}
for_each(coll.begin(), coll.end(), PrintInt());
cout << endl;
for_each(coll.begin(), coll.end(), PrintInt2(10));
cout << endl;
for_each(coll.begin(), coll.end(), Func);
cout << endl;
for_each(coll.begin(), coll.end(), p);
cout << endl;
return 0;
}STL —— for_each与仿函数、函数指针
最新推荐文章于 2024-09-06 22:22:50 发布
本文展示了一段使用C++中模板函数和迭代器操作向量的代码示例,包括自定义打印函数、自定义偏移打印函数、普通函数应用以及通过模板函数进行迭代操作。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
ACE-Step
音乐合成
ACE-Step
ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言
754

被折叠的 条评论
为什么被折叠?



