Re: Using mem_fun_ref on overloaded member function.
Thanks for your help SG. The functor approach worked for me.
I modified it by making a template function "for_all" instead to avoid
needing the bind2nd call, as well as having to specify the argument
type to appendor.
Here's my final version (please comment if you see a problem -I'm
trying to learn templates);
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
template<typename T> class appendor{
public:
template<class A> T& operator ()(T& t,const A& a) const { return
t.append(a);}
template<class A> T& operator()(T& t,A& a) const { return t.append
(a); }
};
template<class V,class F,class A>void for_all(V& vec,const F&
func,const A& arg){
for (typename V::iterator it=vec.begin(); it != vec.end(); it ++){
func(*it,arg);
}
}
int main()
{
vector<string> vs;
vs.push_back(string("One"));
vs.push_back(string("Two"));
for_each(vs.begin(),vs.end(),bind2nd(mem_fun_ref
(&string::push_back),'s'));
cout << vs[0] << " and " << vs[1] << endl; //prints Ones and Twos
for_all(vs,appendor<string>(),"ome");
for_all(vs,appendor<string>(),string(5,'!'));
cout << vs[0] << " and " << vs[1] << endl ; ////Onesome!!!!! and
Twosome!!!!!
}