码迷,mamicode.com
首页 > 编程语言 > 详细

STL - 函数作为算法的参数

时间:2015-09-06 12:54:00      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

函数作为参数,相当于C++的函数指针, C#的委托

for_each函数参数:

#include <iostream>
#include <algorithm>
#include <vector>
#include "FuncParamTest.h"
#include "../../Core/ContainerUtil.h"

using namespace std;

void print(int elem)
{
    cout << elem <<  ;
}

void FuncParamTest::forEachFunc()
{
    vector<int> coll;

    for (int i = 1; i <= 9; ++i)
    {
        coll.push_back(i);
    }

    // print all elements
    for_each(coll.cbegin(), coll.cend(), print);
    cout << endl;
}

void FuncParamTest::run()
{
    forEachFunc();
}

运行结果:
1 2 3 4 5 6 7 8 9

 

STL - 函数作为算法的参数

标签:

原文地址:http://www.cnblogs.com/davidgu/p/4785062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!