常用遍历算法for_each
发布日期:2022-03-04 12:48:34 浏览次数:27 分类:技术文章

本文共 434 字,大约阅读时间需要 1 分钟。

黑马243

#include
#include
#include
#include
using namespace std;
//常用遍历算法 for_each

//普通函数

void print01(int val)
{
cout << val << " ";
}
//仿函数
class print02
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vectorv;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), print01);//普通函数直接使用函数名

cout << endl;for_each(v.begin(), v.end(), print02());//仿函数要使用匿名对象

}

int main()
{
test01();
}

转载地址:https://blog.csdn.net/qq_43752920/article/details/120536459 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:逻辑仿函数 逻辑非
下一篇:常用遍历算法 transform

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月12日 23时22分47秒