|  | adapter function for for_each |  | |
| | | dominolog |  |
| Posted: Wed Aug 20, 2008 10:28 pm Post subject: adapter function for for_each |  |
Hello
I've got following container:
std::vector<boost::shared_ptr<std::string> > strings; strings.push_back( boost::shared_ptr<std::string> ( new std::string("AAA") ) ); strings.push_back( boost::shared_ptr<std::string> ( new std::string("BBB") ) ); strings.push_back( boost::shared_ptr<std::string> ( new std::string("BBB") ) );
std::for_each( strings.begin(), strings.end(), std::mem_fun( &std::length ) );
Now I want a 0-parameter method from std::string to be called on every element from the sequence. I tried to use std::mem_fun (as above), but it claims as follows:
| Quote: | cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'const std::basic_string<_Elem,_Traits,_Ax> *'
|
The question is - how to construct a correct adapter for it, not using a custom operand class. I want to use only stl stuff.
Thanks
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Greg Herlihy |  |
| Posted: Thu Aug 21, 2008 1:28 pm Post subject: Re: adapter function for for_each |  |
| |  | |
On Aug 20, 3:28 pm, dominolog <dominiktomc...@gmail.com> wrote:
| Quote: | I've got following container:
std::vector<boost::shared_ptr<std::string> > strings; strings.push_back( boost::shared_ptr<std::string> ( new std::string("AAA") ) ); ....
std::for_each( strings.begin(), strings.end(), std::mem_fun( &std::length ) );
Now I want a 0-parameter method from std::string to be called on every element from the sequence. '
The question is - how to construct a correct adapter for it, not using a custom operand class. I want to use only stl stuff.
|
Use std::tr1::mem_fn(). For example:
#include <tr1/memory> #include <tr1/functional> #include <string> #include <vector> #include <algorithm>
using std::string; using std::tr1::shared_ptr; using std::tr1::mem_fn;
int main() { std::vector< shared_ptr<string> > v;
std::for_each(v.begin(), v.end(), mem_fn(&string::length)); }
Greg
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|