Parse (split) a string in C++ using string delimiter (standard C++)

后端 未结 20 2132
时光说笑
时光说笑 2020-11-21 23:44

I am parsing a string in C++ using the following:

using namespace std;

string parsed,input=\"text to be parsed\";
stringstream input_stringstream(input);

i         


        
20条回答
  •  花落未央
    2020-11-22 00:16

    This code splits lines from text, and add everyone into a vector.

    vector split(char *phrase, string delimiter){
        vector list;
        string s = string(phrase);
        size_t pos = 0;
        string token;
        while ((pos = s.find(delimiter)) != string::npos) {
            token = s.substr(0, pos);
            list.push_back(token);
            s.erase(0, pos + delimiter.length());
        }
        list.push_back(s);
        return list;
    }
    

    Called by:

    vector listFilesMax = split(buffer, "\n");
    

提交回复
热议问题