#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <iterator>
template < typename OUTPUT_ITERATOR >
OUTPUT_ITERATOR split( const std:: string & str, const std:: string & delimiters,
OUTPUT_ITERATOR dest )
{
std:: string :: size_type pos = str.find_first_of ( delimiters) ;
std:: string :: size_type start = 0 ;
while ( pos ! = std:: string :: npos )
{
std:: string token = str.substr ( start, pos- start ) ;
if ( ! token.empty ( ) ) * dest++ = token ;
start = pos+ 1 ;
pos = str.find_first_of ( delimiters, start ) ;
}
if ( start < str.size ( ) ) * dest++ = str.substr ( start) ;
return dest ;
}
std:: vector < std:: string > split( const std:: string & str, const std:: string & delimiters )
{
std:: vector < std:: string > result ;
split( str, delimiters, std:: back_inserter ( result) ) ;
return result ;
}
int main( )
{
const std:: string jabber = "`Twas brillig, and the slithy toves\n "
"Did gyre and gimble in the wabe:\n "
"All mimsy were the borogoves,\n "
"And the mome raths outgrabe.\n " ;
const std:: string delimiters = "\n ,:.!" ;
int n = 0 ;
for ( const std:: string & token : split( jabber, delimiters ) )
std:: cout << ++ n << ". " << token << '\n ' ;
std:: cout << '\n ' ;
split( jabber, delimiters, std:: ostream_iterator < std:: string > ( std:: cout , " | " ) ) ;
std:: cout << "\n \n " ;
const char cstr[ ] = "Beware the Jabberwock, my son!\n "
"The jaws that bite, the claws that catch!\n "
"Beware the Jubjub bird, and shun\n "
"The frumious Bandersnatch!\n " ;
split( cstr, delimiters, std:: ostream_iterator < std:: string > ( std:: cout , " | " ) ) ;
std:: cout << '\n ' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPGl0ZXJhdG9yPgoKdGVtcGxhdGUgPCB0eXBlbmFtZSBPVVRQVVRfSVRFUkFUT1IgPgpPVVRQVVRfSVRFUkFUT1Igc3BsaXQoIGNvbnN0IHN0ZDo6c3RyaW5nJiBzdHIsIGNvbnN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