#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> //for_each()
class classTest
{
public:
int Dosomething()
{
cout<< "output from method Dosomething !"<<'\n' ;
return 0;
}
};
int DoSome(classTest *cl)
{
return cl -> Dosomething();
}
int main()
{
vector <classTest*> vT;
for(int i=0;i<13;++i)
{
classTest * t = new classTest;
vT.push_back(t);
}
//對容器中的元素進(jìn)行dosomething的操作
for(int i=0;i<vT.size();++i)
vT.at(i)->Dosomething();
//使用迭代器訪問所有的元素
for(vector<classTest*>::iterator it = vT.begin();it != vT.end(); ++it)
{
(*it) -> Dosomething();
}
//若不自己寫循環(huán)蓄诽,用for_each()
//先定義一個函數(shù)Dosome()
for_each(vT.begin(), vT.end(), &DoSome);
cout<<'\n';
//若不想調(diào)用DoSome()函數(shù)呢
for_each(vT.begin(), vT.end(), std::mem_fun( &classTest::Dosomething) );
return 0;
}
mem_fun_ref的作用和用法跟mem_fun一樣仑氛,唯一的不同就是:當(dāng)容器中存放的是對象實體的時候用mem_fun_ref锯岖,當(dāng)容器中存放的是對象的指針的時候用mem_fun甫何。