for_each()事实上是個 function template,其实质如下  [effective STL item 41]

template<typename InputIterator, typename Function>
Function for_each(InputIterator beg, InputIterator end, Function f) {
while(beg != end)
f(*beg++);
}
能看懂吧!!!

Object Oriented 与for_each 搭配

1、不传入参数,使用function object

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std; struct Play
{
void operator () (int i)
{
cout<<i<<endl;
}
}; int main()
{
int a[] = { , , , };
vector<int> vc(a, a+sizeof(a)/sizeof(int));
for_each(vc.begin(), vc.end(), Play());
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std; struct Play
{
Play()
{
cout<<"new a Play"<<endl;
}
Play(const Play&)
{
cout<<"new a copy Play"<<endl;
}
void operator () (int i)
{
cout<<i<<endl;
}
~Play()
{
cout<<"dispose a Play"<<endl;
}
}; int main()
{
int a[] = { , , , };
vector<int> vc(a, a+sizeof(a)/sizeof(int));
for_each(vc.begin(), vc.end(), Play());
cout<<"See something"<<endl;
}
结果如下:
new a Play 




new a copy Play 
dispose a Play 
dispose a Play 
See something

可以看到这个过程有两个Play对象生成,但是,用于输出元素的却是第一个对象(重载() 操作符),为什么? 
这时候回去看for_each的源码,就会发现,它的返回值是function,以我的猜测,应该是这样的。

Play() 生成一个临时的匿名的Play对象,传入for_each 函数里,然后执行完for_each 函数后,return一个function时,Play用复制构造函数生成一个Play对象,然后两个Play对象的生命周期都结束,于是依次销毁。

2、传入参数

可以通过构造函数的技巧传入参数

#include<iostream>
#include<vector>
#include<algorithm>
#include<typeinfo>
using namespace std; struct Play
{
const char* str;
Play(const char* s):str(s) {}
void operator () (int i)
{
cout<<str<<i<<endl;
}
}; int main()
{
int a[] = { , , , };
vector<int> vc(a, a+sizeof(a)/sizeof(int));
for_each(vc.begin(), vc.end(), Play("Element:")); //其实 还是关键看 Play函数如何实现的!
} 结果:
Element:1                                                                                                                                                                          
Element:3                                                                                                                                                                          
Element:4                                                                                                                                                                          
Element:5                                                                                                                                                                          

Member function 与 for_each 搭配

1、不传入参数

通过mem_fun_ref() 这个funtion adapater 将 member funtion 转成 function object。

先到这里吧 下次 继续    !!!!

c++ for_each( )学习的更多相关文章

  1. algorithm 学习之 for_each

    对于algorithm里面的函数使用不算多,但是用过之后才发现,之前写过很多多余的代码,所以打算系统的学习使用下algorithm里的东西,首先就是for_each. 先看下for_each的定义: ...

  2. C++ STL 学习 :for_each与仿函数(functor)

    简单来将,仿函数(functor)就是一个重载了"()"运算符的struct或class,利用对象支持operator()的特性,来达到模拟函数调用效果的技术. 我们平时对一个集合 ...

  3. [C/C++] C/C++延伸学习系列之STL及Boost库概述

    想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...

  4. 给深度学习入门者的Python快速教程 - 基础篇

    实在搞不定博客园的排版,排版更佳的版本在: https://zhuanlan.zhihu.com/p/24162430 Life is short, you need Python 人生苦短,我用Py ...

  5. STL学习之路

    本文面向的读者:学习过C++程序设计语言(也就是说学习过Template),但是还没有接触过STL的STL的初学者.这实际上是我学习STL的一篇笔记,老鸟就不用看了. 什么是泛型程序设计 我们可以简单 ...

  6. STL学习小结

    STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些" ...

  7. STL笔记(5)条款49:学习破解有关STL的编译器诊断信息

    STL笔记(5)条款49:学习破解有关STL的编译器诊断信息 条款49:学习破解有关STL的编译器诊断信息 用一个特定的大小定义一个vector是完全合法的, vector<int> v( ...

  8. 【STL源码学习】STL算法学习之一

    第一章:引子 STL包含的算法头文件有三个:<algorithm><numeric><functional>,其中最大最常用的是<algorithm>, ...

  9. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

随机推荐

  1. 101334E Exploring Pyramids

    传送门 题目大意 看样例,懂题意 分析 实际就是个区间dp,我开始居然不会...详见代码(代码用的记忆化搜索) 代码 #include<iostream> #include<cstd ...

  2. SPOJ GSS7 - Can you answer these queries VII

    板的不能再板,链剖+线段树或者是LCT随便维护. 感觉唯一要注意的是跳链的时候要对$x$向上跳和$y$向上跳的情况分开讨论,而不能直接$swap$,因为只有两段接触的端点才能相互合并,而且每一次向上跳 ...

  3. [译]Javascript中的mutators

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  4. 提取pfx证书公钥和私钥

    从pfx提取密钥信息,并转换为key格式(pfx使用pkcs12模式补足) 1.提取密钥对(如果pfx证书已加密,会提示输入密码.) openssl pkcs12 -in 1.pfx -nocerts ...

  5. NHibernate NHibernate使用时误区

    NHibernate使用时误区 一.异常: 出现org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1异常的原因: ...

  6. C++日志模块实现的经验之谈

    以类的方式对日志模块进行封装,可创建一个单实例的接口或创建一个全局的日志对象指针,同时提供相应的对外写日志接口. 写日志的接口采用可变参数来建立,可使用va_list类型和##args参数,同时在写日 ...

  7. c++语言的学习笔记代码与笔记注释《面向对象部分》

    #include <iostream> /*这是C++中关于面向对象部分的具体笔记和代码 */ //定义类的语法形式; //类中的成员项目之间相互引用,直接使用成员; //类外引用成员的时 ...

  8. Centos7 调整磁盘空间

    1. 查看磁盘空间占用情况:  df -h 可以看到 /home 有很多剩余空间, 而节点较少. 2. 备份 /home 下的内容: cp -r /home/ homebak/ 3. 关闭home进程 ...

  9. winform只能打开一个子窗口

    源地址:https://zhidao.baidu.com/question/1511266887807047660.html 指定弹出的子窗口为模态窗口就可以了,这样在子窗口没有关闭前,是不能操作父窗 ...

  10. js window.open()打开的页面关闭后刷新父页面

    function test(){ var winObj = window.open(URL); var loop = setInterval(function(){ if(winObj.closed) ...