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. fibnacci数列的两种实现(递归实现和循环实现)

    //一切尽在规律中,认真观察,你会明白更多... using System;using System.Collections.Generic;using System.Linq;using Syste ...

  2. Entity Framework Tutorial Basics(12):Model First

    Model First development with Entity Framework: In the Model First approach, you create Entities, rel ...

  3. Owin password

    一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorization(授权),而不是Authentication(认证). ...

  4. CENTOS 7 和 JDK 添加中文字体

    写在前面的话 当运维总是遇到各种奇奇怪怪的问题,比如新的 JAVA 项目上线,login 界面有个验证码,结果部署后发现,要么显示的奇奇怪怪,要么压根不显示. 或者在使用一些开源的 JAVA 项目的时 ...

  5. 「BZOJ 3994」「SDOI 2015」约数个数和「莫比乌斯反演」

    题意 设\(d(x)\)为\(x\)的约数个数,求\(\sum_{i=1}^{n}\sum_{j=1}^{m}d(ij)\). 题解 首先证个公式: \[d(ij) = \sum_{x|i}\sum_ ...

  6. Wannafly挑战赛28B(DP,思维,字符串)

    #include<bits/stdc++.h>using namespace std;int n;int nxt[3][100007];char buff[100007];const ch ...

  7. c语言参考书籍

    很惭愧没能把c++学的很好,毕竟离开始工作只有2年时间,对自己要求不要过高,慢慢来吧.话说知道自己的不足,以后要更加抓紧了!fighting~ 现在计划着把c语言给学习一下了,当然这次指的是深入地学习 ...

  8. [bzoj2743][HEOI2012]采花(树状数组)

    题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...

  9. Chrome 67 以后版本无法离线安装crx插件

    原文链接:https://blog.csdn.net/wanwuguicang/article/details/80716178 升级了Chrome后无法离线安装扩展 如图: 谷歌自Chrome 67 ...

  10. P5056 【模板】插头dp

    \(\color{#0066ff}{ 题目描述 }\) 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? \(\color{#0066ff}{输入格式}\) 第1 ...