Over a million developers have joined DZone.

C++ Tokenize Using Stl

·
// description of your code here


typedef string::size_type S_T;

//trim指示是否保留空串,默认为保留。   
int tokenize(const string& src, string tok,vector &tokens,bool trim=false, string null_subst="")
{       
        if( src.empty() || tok.empty() )
                return -1;

        
        S_T pre_index = 0, index = 0, len = 0;   
        while( (index = src.find_first_of(tok, pre_index)) != string::npos )
        {       
                if( (len = index-pre_index)!=0 )   
                        tokens.push_back(src.substr(pre_index, len));
                else if(trim==false)   
                        tokens.push_back(null_subst);
                pre_index = index+1;
        }   
        string endstr = src.substr(pre_index);   
        if( trim==false ) tokens.push_back( endstr.empty()?
                        
                        null_subst:endstr );   
        else if( !endstr.empty() ) tokens.push_back(endstr);
        
        return 0;
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}