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

后端 未结 20 2123
时光说笑
时光说笑 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:10

    std::vector parse(std::string str,std::string delim){
        std::vector tokens;
        char *str_c = strdup(str.c_str()); 
        char* token = NULL;
    
        token = strtok(str_c, delim.c_str()); 
        while (token != NULL) { 
            tokens.push_back(std::string(token));  
            token = strtok(NULL, delim.c_str()); 
        }
    
        delete[] str_c;
    
        return tokens;
    }
    

提交回复
热议问题