#include <string>
#include <deque>

template <class _CharT, class _Traits, class _Alloc> 
void tokenize( 
	const basic_string<_CharT, _Traits, _Alloc> &str, 
	const basic_string<_CharT, _Traits, _Alloc> &delims, 
	deque< basic_string<_CharT, _Traits, _Alloc> > &found_tokens )
{
	basic_string<_CharT, _Traits, _Alloc>::size_type walk_pos = 0;

	while( walk_pos != basic_string<_CharT, _Traits, _Alloc>::npos && walk_pos < str.length() )
	{
		basic_string<_CharT, _Traits, _Alloc>::size_type token_pos = 0, token_len = 0;

		basic_string<_CharT, _Traits, _Alloc>::size_type delim_pos = str.find_first_of( delims, walk_pos );

		if( delim_pos == basic_string<_CharT, _Traits, _Alloc>::npos )
		{
			// no more delims, a token starts at walk_pos
			
			token_pos = walk_pos;
			token_len = str.length() - token_pos;
			
			walk_pos += token_len;
		}
		else if( delim_pos > walk_pos )
		{
			// more tokens / delims left, but a token starts at walk_pos

			token_pos = walk_pos;
			token_len = delim_pos - token_pos;
			
			walk_pos = delim_pos;
		}
		else if( delim_pos == walk_pos )
		{
			// delimiters start at walk_pos
			
			walk_pos = str.find_first_not_of( delims, walk_pos );

			if( walk_pos == basic_string<_CharT, _Traits, _Alloc>::npos )
			{
				// only delims left in str, no more tokens
				break;
			}
			else
			{
				// more tokens left
				continue;
			}
		}

		found_tokens.push_back( str.substr( token_pos, token_len ) );
	}
}

