【C++】 string split

C++ 的字符串分割

    vector<string_view> split(const string_view& str,char  trim) {
    
    
        int n = str.size();
        vector<string_view> res;
        int pos = 0;
        while (pos < n) {
    
    
            while (pos < n && str[pos] == trim) {
    
    
                pos++;
            }
            if(pos < n) {
    
    
                int curr = pos;
                while(pos < n && str[pos] != trim) {
    
    
                    pos++;
                }
                res.emplace_back(str.substr(curr, pos-curr));
            }
        }

        return res;
    }

猜你喜欢

转载自blog.csdn.net/Fuel_Ming/article/details/128908146