mem_fun以及mem_fun_ref与bindst和bind2nd配合使用
https://blog.csdn.net/weixin_44980842/article/details/122754095
怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是用循环来实现。
比如有如下的一个类:
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。
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <boost/filesystem.hpp>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <typeinfo>
#include <algorithm>
#include <list>
#include <set>
#include <future>
using namespace std;
#include<iostream>
#include<thread>
#include <mutex>
#include <signal.h>
#include <fstream>
#include <condition_variable>
using namespace std;
#include <ctime>
#include <iostream>
#include <string>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/asio.hpp>
#include <signal.h>
#include <boost/optional.hpp>
#include <bits/stdc++.h>
#include <boost/optional.hpp>
#include <iterator>
#include <memory>
#include <functional>
#include <cassert>
#include <boost/program_options.hpp>
#include <boost/variant.hpp>
#include <boost/tokenizer.hpp>
class A
{
public:
int sum=0;
int num=0;
A(int i):num(i){}
bool f(int i)
{
return i == 20;
}
void test(int i)
{
cout<<i<<endl;
sum+=i;
}
void test1(int i)
{
num+=i;
cout<<"i="<<i<<",num="<<num<<endl;
}
void test2(const A a)
{
cout<<a.num<<endl;
sum+=a.num;
}
};
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
A a(0);
//mem_fun ->(class指针pointer,参数)
//mem_fun_ref--》(class object(非指针),参数)
//bind1st 绑定第一个参数如果&a(地址),需要使用mem_fun,如果指定第一个参数一般是mem_fun
//bind2st 绑定第二参数,第一参数为指针使用mem_fun,第一参数是class object 使用mem_fun_ref
int c = count_if(v.begin(), v.end(), bind1st(mem_fun(&A::f), &a));//(&a)->f(int)
for_each(v.begin(), v.end(), bind1st(mem_fun(&A::test), &a));//(&a)->test(int)
cout<<a.sum<<endl;
vector<A> va{A(1),A(10),A(20)};
for_each(va.begin(),va.end(),bind1st(mem_fun(&A::test2),&a));//&(a)->test2()
cout<<a.sum<<endl;
for_each(va.begin(),va.end(),bind2nd(mem_fun_ref(&A::test1),12));//va[].test1(123);
// struct genterateFun
// {
// FuncPtr f
// genterateFun(Function _f):f(_f){}
// type operator()(A* pA,int x)
// {
// return pA->f(x);
// }
// };
// assume the input parameter from vector is P
// bind1st(F,Y) ==>// return a functor operator() will call genterateFunc(Y,P) cout << c << endl; // 1
return 0;
}

浙公网安备 33010602011771号