DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C++ Tokenize Using Stl

01.03.2009
| 4762 views |
  • submit to reddit
        // description of your code here

typedef string::size_type S_T;

//trim指示是否保留空串,默认为保留。   
int tokenize(const string& src, string tok,vector<string> &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;
}