Platinum Partner

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;
}
{{ tag }}, {{tag}},

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

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}