#include #include #include #include #include using namespace std; inline std::vector split( const std::string& s, const std::string& f, const bool keep = false ) { std::vector temp; if ( f.empty() ) { temp.push_back( s ); return temp; } typedef std::string::const_iterator iter; const iter::difference_type f_size( distance( f.begin(), f.end() ) ); iter i( s.begin() ); for ( iter pos; ( pos = search( i , s.end(), f.begin(), f.end() ) ) != s.end(); ) { temp.push_back( std::string( i, pos ) ); advance( pos, f_size ); i = pos; if ( keep ) { temp.push_back( f ); } } temp.push_back( std::string( i, s.end() ) ); return temp; } int main() { const string s( "http://somesite.com/src/compose.php?send_to=*T&subject=*S&body=*B&send_to_cc=*C&send_to_bcc=*A" ); const vector test( split( s, "*" ) ); copy( test.begin(), test.end(), ostream_iterator( cout, "\n" ) ); cout << endl; const vector test2( split( s, "*", true) ); copy( test2.begin(), test2.end(), ostream_iterator( cout, "\n" ) ); } /* g++ -Wall -Wextra this.cpp -o this -O3 -msse3 -mtune=i686 -s */