C++ 實現string的split

// 分割字符串                                                                        
void split(const string &line, const string &sep, vector<string> &res){              
    size_t start = 0;                                                                
    size_t index = line.find_first_of(sep, start);                                   
    while( index != string::npos ){                                                  
        if( index - start > 0 )                                                      
            res.push_back( line.substr(start, index - start) );                      
        start = index + 1;                                                           
        index = line.find_first_of(sep, start);                                      
    }                                                                                
    if( index - start > 0 )                                                          
        res.push_back( line.substr(start, index - start) );                          
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章