for_each用法
for_each()是个function template #include <algorithm>头文件说明
template<class _InIt,
class _Fn1> inline
void _For_each(_InIt _First, _InIt _Last, _Fn1& _Func)
{ // perform function for each element
for (; _First != _Last; ++_First)
_Func(*_First);
} 由以上source可知,for_each()只能配合global function和function object。 #include <iostream>
#include <stdlib.h>
#include "vector"
#include "map"
#include <algorithm>
#include <atlstr.h>
#include <xfunctional>
using namespace std; //单一类对象
class CClassObj
{
public:
CClassObj(void){}
~CClassObj(void){}
void PrintElem(){
std::cout << "mem_fun_ref" << std::endl;
}
};
class ClassController
{
protected:
vector<CClassObj> _ObjVec;
public:
ClassController(void){}
~ClassController(void){}
void AddClass(CClassObj cObj){
_ObjVec.push_back(cObj);
}
void PrintElem(){
for_each(_ObjVec.begin(), _ObjVec.end(), mem_fun_ref(&CClassObj::PrintElem));
}
}; //多态
class AbstactClass
{
public:
virtual void PrintElem(){
std::cout << "Abstact mem_fun 1" << std::endl;
}
}; class MyClass1 : public AbstactClass{};
class MyClass2 : public AbstactClass
{
public:
void PrintElem(){
std::cout << "Abstact mem_fun 2" << std::endl;
}
}; class ClassCont
{
protected:
vector<AbstactClass*> _ObjVec; public:
void AddClass(AbstactClass& obj){
_ObjVec.push_back(&obj);
}
void PrintElem(){
for_each(_ObjVec.begin(), _ObjVec.end(), mem_fun(&AbstactClass::PrintElem));
}
}; struct Mystruct
{
int cnt;
string str;
Mystruct()
{
cnt = ;
str = "str";
}
}; //函数调用对象 for_each()只能配合global function和function object。
/*void Print(const Mystruct stru){ cout << stru.cnt << ", " << stru.str.c_str() << endl; }*/
void Print(const Mystruct stru, const char* str){ cout << str << stru.cnt << ", " << stru.str.c_str() << endl; } struct PrintStru
{
string _str = "";
PrintStru(string str) : _str(str){
}
void operator()(string &str){
cout << _str.c_str() << str.c_str() << endl;
}
};
int main()
{
/* Add a(1, 2), b(3, 4);*/
/* std::cout << a << " + " << b << " = " << (a + b) << std::endl;*/
vector<Mystruct> vecStru;
Mystruct stru;
vecStru.push_back(stru);
vecStru.push_back(stru);
/*for_each(vecStru.begin(), vecStru.end(), [](const Mystruct stru){cout << stru.cnt << ", " << stru.str.c_str() << endl; });//lambda可调用对象*/ //Procedure Based與for_each()搭配
/*for_each(vecStru.begin(), vecStru.end(), Print);//print: 不传入参数*/
for_each(vecStru.begin(), vecStru.end(), bind2nd(ptr_fun(Print), "elem: "));//print: 传入参数 //Object Oriented與for_each()搭配
string Arr[] = { "", "", "", "", "" };
vector<string> vecStr(Arr, Arr + sizeof(Arr) / sizeof(string));
for_each(vecStr.begin(), vecStr.end(), PrintStru(""));//print: 不传入参数
//若使用function object,也可以將參數傳給PrintStru(),透過constructor的技巧接收參數。
for_each(vecStr.begin(), vecStr.end(), PrintStru("Elem: "));//print: 不传入参数 ClassController cConT;
cConT.AddClass(CClassObj());
cConT.AddClass(CClassObj());
cConT.PrintElem(); //多态
ClassCont cT;
cT.AddClass(MyClass1());
cT.AddClass(MyClass2());
cT.PrintElem(); }
借鉴:http://www.cnblogs.com/Purple_Xiapei/archive/2012/05/27/2520477.html
for_each用法的更多相关文章
- 匿名函数和for_each用法
匿名函数,C++11的 for_each 用法 #include <iostream> #include <algorithm> #include "testClas ...
- for_each 用法!
class MapTest:public CapTest{ private: set <string> MyTestContain; typedef pair <string,int ...
- CPP STL学习笔记
STL的概念 源地址 https://www.ev0l.art/index.php/archives/15/ <li> Iterator (迭代器)<li> Containe ...
- 《STL源码剖析》学习半生记:第一章小结与反思
不学STL,无以立.--陈轶阳 从1.1节到1.8节大部分都是从各方面介绍STL, 包括历史之类的(大致上是这样,因为实在看不下去我就直接略到了1.9节(其实还有一点1.8.3的内容)). 第一章里比 ...
- c++11的for新用法 (重新练习一下for_each)
看到手册的代码里面有个for的很奇怪的用法,用了一把 http://www.cplusplus.com/reference/unordered_set/unordered_set/insert/ ...
- C++11之for循环的新用法
C++使用如下方法遍历一个容器: #include "stdafx.h" #include<iostream> #include<vector> int m ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- c++ bind1st 和 bind2nd的用法
std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码. 代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用 ...
- c++ algorithm 的用法
1 , accumulate()template<class _II, class _Ty> inline_Ty accumulate(_II _F, _II _L, _Ty _V){fo ...
随机推荐
- 【BZOJ】2020: [Usaco2010 Jan]Buying Feed, II (dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2020 和背包差不多 同样滚动数组 f[j]表示当前位置j份食物的最小价值 f[j]=min(f[j- ...
- grep检索文本
grep [OPTIONS] PATTERN [FILE...] grep zifuchuan * 不行的话来一个: grep zifuchuan */* 不行的话再来一个: grep zifuc ...
- django form POST方法提交表达
之前就着手开始尝试用django来简化web开发的流程周期,果不其然,速度还行,当然前期的产品那就相当粗糙了.举例来说,就连最基本的登录都是抄别人的,最可怕的是用GET方法提交表单,今天就尝试解决这个 ...
- jquery获取checkbox状态
$("#id").is(":checked"); 返回true false
- RedHat Ent 6.5 64bit编译安装hadoop2.4.1
RedHat Ent 6.5 64bit编译安装hadoop2.4.1 感谢原帖:http://blog.csdn.net/w13770269691/article/details/16883663/ ...
- Android上传图片(PHP服务器)
原理 Android客户端模拟一个HTTP的Post请求到服务器端,服务器端接收相应的Post请求后,返回响应信息给给客户端. PHP服务器 <?php move_uploaded_file($ ...
- K-Means算法Demo
简介:本Demo是参照这个网站上的Demo自己用Java实现的.将Java打包为Jar,再将Jar转为exe,源代码及程序Demo下载请点我. K-Means算法简介 我尽量用通俗易懂但不规范的语言来 ...
- MySQL Error: Illegal mix of collations for operation 'concat'
在使用concat连接字符串时出现错误:MySQL Error: Illegal mix of collations for operation 'concat' 原因:字段操作默认为UTF8的编码, ...
- JDK动态代理具体解释
首先说一下动态代理和静态代理的差别: 静态代理:是预先写好或由特定工具自己主动生成的代码.再对其编译.在程序执行前.代理类的.class文件就已经存在了. 动态代理:代理是在程序执行时,运用反射机制动 ...
- SQL语法:查询此表有另外一个表没有的数据
select bei.ExamItem_Code2,*from PeisPatientExamItem ppeijoin PeisPatientFeeItem ppfion ppfi.ID_Patie ...