Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++

adapter function for for_each

 
Jump to:  
 
dominolog
PostPosted: 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
PostPosted: 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! ]
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

roll up Interactive agency Socken Herstellung zaluzje poker online