c ++ tokenize std string

Dan*_*ore 6 c++ tokenize strtok

可能重复:
如何在C++中对字符串进行标记?

您好我想知道如何用strtok标记std字符串

string line = "hello, world, bye";    
char * pch = strtok(line.c_str(),",");
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

error: invalid conversion from ‘const char*’ to ‘char*’
error: initializing argument 1 of ‘char* strtok(char*, const char*)’
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种快速简便的方法,因为我认为它不需要太多时间

Pio*_*ycz 14

我总是用getline这些任务.

istringstream is(line);
string part;
while (getline(is, part, ','))
  cout << part << endl;
Run Code Online (Sandbox Code Playgroud)


Pet*_*ker 10

std::string::size_type pos = line.find_first_of(',');
std::string token = line.substr(0, pos);
Run Code Online (Sandbox Code Playgroud)

找到下一个标记,重复find_first_of但开始pos + 1.