c++ split 方法(转)

vector<string> split(const string &s, const string &seperator, vectorstring> &result){
    /*vector result;
    typedef string::size_type string_size;
    string_size i = 0;
    while(i != s.size()){    //找到字符串中首个不等于分隔符的字母;
        int flag = 0;
        while(i != s.size() && flag == 0){
            flag = 1;
            for(string_size x = 0; x < seperator.size(); ++x)
                if(s[i] == seperator[x]){
                    ++i;
                    flag = 0;
                    break;
                }
        }
        //找到又一个分隔符,将两个分隔符之间的字符串取出;
        flag = 0;
        string_size j = i;
        while(j != s.size() && flag == 0){
            for(string_size x = 0; x < seperator.size(); ++x)
                if(s[j] == seperator[x]){
                    flag = 1;
                    break;
                }
            if(flag == 0)
                ++j;
        }
        if(i != j){
            result.push_back(s.substr(i, j-i));
            i = j;
        }
    }
    return result;*/
    string str = s;
    string substring;
    string::size_type start = 0, index;
    
    do
    {
        index = str.find_first_of(seperator,start);
        if (index != string::npos)
        {
            substring = str.substr(start,index-start);
            result.push_back(substring);
            start = str.find_first_not_of(seperator,index);
            if (start == string::npos) return result;
        }
    }while(index != string::npos);
    
    //the last token
    substring = str.substr(start);
    result.push_back(substring);
    return result;
}

注释掉得是另一方法

转载于:https://www.cnblogs.com/pengyingh/articles/4250605.html

你可能感兴趣的:(c/c++)