Re: how to get stl algorithms to accept functions?
In article <1186466690.947791.287650@57g2000hsv.googlegroups.com>,
jraddison@gmail.com wrote:
I'm trying to use remove_if with boost::xpressive::regex_match but I
can't figure out how to convince remove_if to accept regex_match. I've
tried casting this and that, but I did not really get anywhere. Is
there a good discussion on this somewhere? In the code below I have
two options one with bind2nd and the other with lambda _1. Can anyone
enlighten me on how to get either or both to work? Thanks ...
// g++ -o xxxx xxxx.cpp -I/home/jra/usr/roars-ext-libs/boost/include -
L/home/jra/usr/roars-ext-libs/boost/lib -lboost_filesystem-gcc41
#include <boost/xpressive/xpressive.hpp>
#include <boost/filesystem.hpp>
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <algorithm>
#include <functional>
namespace bfs = boost::filesystem;
namespace xpr = boost::xpressive;
using namespace boost::lambda;
using namespace std;
int main( )
{
char *data[] = {
"abc123.efg",
"abc.eft",
"abc23.efg",
"abc4567.efg",
"abc",
"uvwxy1.ef",
"123abc.ef",
"234." };
list<string> strgs( data, data + 8 );
xpr::sregex rex = xpr::sregex::compile( "abc[0-9]+\\.efg" );
bool match = xpr::regex_match( string( "abc56.efg" ), rex );
cout << "match = " << match << endl; // works
// strgs.remove_if( bind2nd( xpr::regex_match, re ) ); // opt 1
strgs.remove_if( xpr::regex_match( _1, rex ) ); // opt 2
return 0;
}
In this particular case, the compiler is unable to determine the type
for the function. This will work though:
using namespace std;
namespace xpr = boost::xpressive;
bool regex_match( string s, xpr::sregex rex )
{
return boost::xpressive::regex_match( s, rex );
}
int main( )
{
char *data[] = {
"abc123.efg",
"abc.eft",
"abc23.efg",
"abc4567.efg",
"abc",
"uvwxy1.ef",
"123abc.ef",
"234." };
list<string> strgs( data, data + 8 );
copy( strgs.begin(), strgs.end(),
ostream_iterator<string>( cout, "\n" ) );
cout << "\n";
strgs.remove_if( bind2nd( ptr_fun( ®ex_match ),
xpr::sregex::compile( "abc[0-9]+\\.efg" ) ) );
copy( strgs.begin(), strgs.end(),
ostream_iterator<string>( cout, "\n" ) );
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]