原文:http://www.cppblog.com/mysileng/archive/2012/12/25/196615.html

引子:

怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是用循环来实现。
比如有如下的一个类:

class ClxECS{
public:
    int DoSomething() { 
    cout << "Output from method DoSomething!" << endl; // 这里以输出一句话来代替具体的操作
    return 0;
    };
};

现在定义如下一个vector:

vector<ClxECS*> vECS;

for(int i = 0; i < 13; i++){
    ClxECS *pECS = new ClxECS;
    vECS.push_back(pECS);
}

如果要对容器vECS中的所有对象都进行DoSomething()的操作,那么下面的循环可能是首先想到的方案:

for(int i = 0; i < vECS.size(); i++)
    vECS.at(i)->DoSomething();

当然,我们也可以用iterator:

for(vector<ClxECS*>::iterator it = vECS.begin(); it != vECS.end(); ++it)
    (*it)->DoSomething();

但是,有很多C++的高手和牛人们都会给我们一个忠告,那就是:在处理STL里面的容器的时候,尽量不要自己写循环。
那么,我们就只好用STL算法里面的for_each了。
首先,添加如下一个函数:

int DoSomething(ClxECS *pECS)
{
    return pECS->DoSomething();
}
然后就可以用for_each来实现我们想要的功能:

for_each(vECS.begin(), vECS.end(), &DoSomething);
说了半天,似乎跟mem_fun和mem_fun_ref没有什么关系。其实,说那么多都是为了引出mem_fun和mem_fun_ref。在用for_each的时候,如果我们不添加上面的那个函数,该怎么办呢?

这个时候就该mem_fun和mem_fun_ref隆重登场了。用如下这一行代码就行了:

for_each(vECS.begin(), vECS.end(), mem_fun(&ClxECS::DoSomething));
实际上就是由迭代器去调用成员函数.

例子:

list<Widget *> lpw;
for_each(lpw.begin(), lpw.end(),mem_fun(&Widget::test)); // pw->test();

vector<Widget> vw;
for_each(vw.begin(), vw.end(),mem_fun_ref(&Widget::test)); // w.test();

成员函数有参数的情况:将值传入,再bind1st为this

std::for_each(m_erased.begin(), m_erased.end(),std::bind1st(std::mem_fun(&SocketSet::_replace_with_last), this));
//相当于this->_replace_with_last(iter)  //iter

两者区别:
mem_fun_ref的作用和用法跟mem_fun一样,唯一的不同就是:

当容器中存放的是对象实体的时候用mem_fun_ref,

当容器中存放的是对象的指针的时候用mem_fun。

【转】STL中mem_fun和mem_fun_ref的用法及区别的更多相关文章

  1. STL中mem_fun和mem_fun_ref的用法

    例如:假设有如下的代码: class Employee { public: int DoSomething(){} } std::vector<Employee> Emps; 假设我们要调 ...

  2. STL中mem_fun与mem_fun_ref的区别[转]

    http://www.cnblogs.com/Purple_Xiapei/archive/2012/05/27/2520483.html STL中mem_fun和mem_fun_ref的用法 分类:  ...

  3. 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别

    Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...

  4. 分组函数group by和Oracle中分析函数partition by的用法以及区别

    1.分组函数group by和Oracle中分析函数partition by的用法以及区别 2.开窗函数.

  5. STL中mem_fun, mem_fun_ref用法

    1.引言 先看一个STL中for_each的用法: #include <iostream> #include <vector> #include <algorithm&g ...

  6. C++ mem_fun 和 mem_fun_ref 的用法

    假设我们有以下的一个类: 另外有一个包含 class A 对象的数组: vector<A> vec; 如何对每一个类的对象调用成员函数print. 做法1: 利用下标 for(int i= ...

  7. STL中find和sort的用法总结

    STL算法 STL 提供能在各种容器中通用的算法(大约有70种),如插入.删除.查找.排序等. 许多算法操作的是容器上的一个区间(也可以是整个容器),因此需要两个参数,一个是区间起点元素的迭代器,另一 ...

  8. STL中map,set的基本用法示例

    本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...

  9. c++中STL中的next_permutation函数基本用法

    对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...

随机推荐

  1. Creating a Mono 3 RPM on CentOS

    Creating a Mono 3 RPM on CentOS A quick guide to creating an rpm of mono 3 from source, starting wit ...

  2. MySQL服务器的SQL模式 (转)

    转自: http://blog.csdn.net/kumu_linux/article/details/8185912 sql_mode的系统变量可以调控MySQL的SQL模式 任何一个客户端可以在不 ...

  3. LeetCode 274

    H-Index Given an array of citations (each citation is a non-negative integer) of a researcher, write ...

  4. U-boot新手入门

    U-boot新手入门 一.编译U-boot 二.U-boot命令详解 1.帮助命令 # help autoscr -run script from memory base -print or set ...

  5. POi写入大批量数据

    直接贴代码: package jp.co.misumi.mdm.batch.common.jobrunner; import java.io.File; import java.io.FileNotF ...

  6. asp.net常见面试题(一)

    1.索引器 class Player { ]; public int this[int index] { get { || index >= ) { ; } else { return arr[ ...

  7. IOS Delegate & protocal

    总结一下: delegate是一个方式,程序组成单元之间分工的一种协调思想 protocal 这个东西不能单独说,要与它相关的两个主要东西一起说,一个是 委托者 通常是VIEW, 一个是被委托者 通常 ...

  8. Ssqlserver 关于Grouping sets

    sqlserver2008之后引入Grouping sets是group by的增强版本,Grouping sets 在遇到多个条件时,聚合是一次性从数据库中取出所有需要操作的数据,在内存中对数据库进 ...

  9. Debian8 加载NTFS 磁盘出错 解决方法

    执行 ntfsfix  /dev/sdb2 // sd*  a代表第一块硬盘 b代表第2块硬盘 数字是分区号 执行后 就可以正常使用了.

  10. eclipse插件hibernate tools安装 爱好者

    eclipse helios(3.6)版 1.启动eclipse 2.选择Help > Install New Software...> 3.添加如下地址:http://download. ...