 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andrew Guest
|
Posted: Thu Dec 22, 2005 3:17 am Post subject: for_each with vector<boost::shared_ptr< > > |
|
|
Hello all:
Is there a standard means of using the for_each algorithm on an STL
container of smart pointers? Here is the solution I have so far, but
it might be obsolete:
// compose.h
#ifndef COMPOSE_MY_H
#define COMPOSE_MY_H
# include <functional>
/* class for composition f(g(x)) */
template<class OP1, class OP2>
class compose_f_gx_t
: public std::unary_function<typename OP2::argument_type,
typename OP1::result_type>
{
private:
OP1 op1; // process: op1(op2(x))
OP2 op2;
public:
compose_f_gx_t(const OP1& o1, const OP2& o2) : op1(o1), op2(o2) { }
typename OP1::result_type operator()(const typename
OP2::argument_type& x) const
{ return op1(op2(x)); }
};
template<class OP1, class OP2>
inline compose_f_gx_t<OP1,OP2>
compose_f_gx(const OP1& o1, const OP2& o2)
{ return compose_f_gx_t<OP1,OP2>(o1,o2); }
#endif COMPOSE_MY_H
// memfun1b.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for for_each
#include <functional> // for mem_fun_ref and mem_fun
#include <boost/shared_ptr.hpp> // for boost::shared_ptr<>
#include "compose.h" // for compose_f_gx
using namespace std;
class Person
{
private:
string first;
string last;
public:
Person(const string& f, const string& l) : first(f), last(l) { }
virtual ~Person() { }
virtual void print() const
{ cout << first << " " << last << endl; }
/* ... */
};
int main()
{
typedef boost::shared_ptr
vector<Ptr> coll;
coll.push_back(Ptr(new Person("Joe","Powers")));
coll.push_back(Ptr(new Person("Tariq","Aslam")));
coll.push_back(Ptr(new Person("Andrew","Henrick")));
coll.push_back(Ptr(new Person("Suzanne","Henrick")));
coll.push_back(Ptr(new Person("Bill","Henrick")));
cout << "Ask each element to print itself: " << endl;
for_each(coll.begin(),coll.end(),
compose_f_gx(mem_fun_ref(&Person::print),mem_fun_ref(&Ptr::operator*)));
cout << endl;
cout << "Ask each element to print itself: " << endl;
for_each(coll.begin(),coll.end(),
compose_f_gx(mem_fun(&Person::print),mem_fun_ref(&Ptr::get)));
cout << endl;
}
This compiles and runs on gcc 3.3 (Apple) and with boost 1.33.1. Two
solutions are given.
Thanks for your input.
Andrew
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Dimov Guest
|
Posted: Fri Dec 23, 2005 12:43 pm Post subject: Re: for_each with vector<boost::shared_ptr< > > |
|
|
Andrew wrote:
[...]
| Quote: | int main()
{
typedef boost::shared_ptr<Person> Ptr;
vector<Ptr> coll;
coll.push_back(Ptr(new Person("Joe","Powers")));
coll.push_back(Ptr(new Person("Tariq","Aslam")));
coll.push_back(Ptr(new Person("Andrew","Henrick")));
coll.push_back(Ptr(new Person("Suzanne","Henrick")));
coll.push_back(Ptr(new Person("Bill","Henrick")));
cout << "Ask each element to print itself: " << endl;
for_each(coll.begin(),coll.end(),
compose_f_gx(mem_fun_ref(&Person::print),mem_fun_ref(&Ptr::operator*)));
|
for_each( coll.begin(), coll.end(), boost::mem_fn( &Person::print )
);
http://www.boost.org/libs/bind/mem_fn.html#Purpose
| Quote: | cout << endl;
cout << "Ask each element to print itself: " << endl;
for_each(coll.begin(),coll.end(),
compose_f_gx(mem_fun(&Person::print),mem_fun_ref(&Ptr::get)));
|
for_each( coll.begin(), coll.end(), boost::bind( &Person::print, _1 )
);
http://www.boost.org/libs/bind/bind.html#with_member_pointers
As for compose_f_gx:
http://www.boost.org/libs/bind/bind.html#nested_binds
http://www.boost.org/libs/bind/bind_as_compose.cpp
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|