c++ for_each( )学习
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
1
3
4
5
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函数如何实现的!
} 结果:
Member function 与 for_each 搭配
1、不传入参数
通过mem_fun_ref() 这个funtion adapater 将 member funtion 转成 function object。
先到这里吧 下次 继续 !!!!
c++ for_each( )学习的更多相关文章
- algorithm 学习之 for_each
对于algorithm里面的函数使用不算多,但是用过之后才发现,之前写过很多多余的代码,所以打算系统的学习使用下algorithm里的东西,首先就是for_each. 先看下for_each的定义: ...
- C++ STL 学习 :for_each与仿函数(functor)
简单来将,仿函数(functor)就是一个重载了"()"运算符的struct或class,利用对象支持operator()的特性,来达到模拟函数调用效果的技术. 我们平时对一个集合 ...
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...
- 给深度学习入门者的Python快速教程 - 基础篇
实在搞不定博客园的排版,排版更佳的版本在: https://zhuanlan.zhihu.com/p/24162430 Life is short, you need Python 人生苦短,我用Py ...
- STL学习之路
本文面向的读者:学习过C++程序设计语言(也就是说学习过Template),但是还没有接触过STL的STL的初学者.这实际上是我学习STL的一篇笔记,老鸟就不用看了. 什么是泛型程序设计 我们可以简单 ...
- STL学习小结
STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些" ...
- STL笔记(5)条款49:学习破解有关STL的编译器诊断信息
STL笔记(5)条款49:学习破解有关STL的编译器诊断信息 条款49:学习破解有关STL的编译器诊断信息 用一个特定的大小定义一个vector是完全合法的, vector<int> v( ...
- 【STL源码学习】STL算法学习之一
第一章:引子 STL包含的算法头文件有三个:<algorithm><numeric><functional>,其中最大最常用的是<algorithm>, ...
- ACM学习
转:ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. US ...
随机推荐
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-005Table per subclass with joins(@Inheritance(strategy = InheritanceType.JOINED)、@PrimaryKeyJoinColumn、)
一.结构 The fourth option is to represent inheritance relationships as SQL foreign key associations. Ev ...
- Android 之 信息通知栏消息Notification
Notification是安卓手机顶部的消息提示 这里我们分别设置两个按钮,来实现顶部消息的发送和取消 功能实现 首先要在主Activity中设置一个通知控制类 NotificationManager ...
- 一个ButtonDemo的实现过程。
来自JDK API 1.6.0: Try this: Click the Launch button to run the Button Demo using Java™ Web Start (dow ...
- Django框架 之 ORM查询操作详解
Django框架 之 ORM查询操作详解 浏览目录 一般操作 ForeignKey操作 ManyToManyField 聚合查询 分组查询 F查询和Q查询 事务 Django终端打印SQL语句 在Py ...
- struts学习记录
see also:http://blog.csdn.net/chenggil10/article/details/5965806#_Toc250472631 0.struts2每一个请求,都new一个 ...
- leetcode 6 ZigZag Converesion
class Solution { public: string convert(string s, int nRows) { if (nRows <= 1) return s; string r ...
- 原型模式与serializable
写原型模式时课件上有一个实现模式是利用可串行化接口实现,然后就发现那个代码(如下),串行化接口里面没有函数,这种接口被曾为标记接口,implements这个接口后就可以对其进行各种流操作了,其实就是O ...
- Java50道经典习题-程序44 偶数的素数和
题目:一个偶数总能表示为两个素数之和.分析:一个偶数可能会有不止一对两个素数之和的情况 例如:20=3+17 20=7+13 import java.util.Scanner; public clas ...
- Axure RP7.0移动互联网产品原型设计 中文pdf扫描版
移动互联网原型设计,简单来说,就是使用建模软件制作基于手机或者平板电脑的App,HTML 5网站的高保真原型.在7.0 之前的版本中,使用Axure RP进行移动互联网的建模也是可以的.比如,对于桌面 ...
- 【转,整理】C# 非托管代码
.Net Framework 是由彼此独立又相关的两部分组成:CLR 和 类库, CLR是它为我们提供的服务,类库是它实现的功能..NET的大部分特性----垃圾收集,版本控制,线程管理等,都使用了C ...