【发布时间】:2019-12-15 23:09:18
【问题描述】:
我正在尝试使用函数 strtok 来标记来自用户的输入,然后在每个单词之间用新行打印出结果。但是,会弹出此错误。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
string str;
char *tokenPtr;
cout << "Enter a sentence : " << endl;
getline(cin, str);
tokenPtr = strtok( str, " " ); // ERROR: No matching function for call to 'strtok'
while( tokenPtr != NULL ) {
cout << tokenPtr << endl;
tokenPtr = strtok( NULL, " " );
}
return 0;
}
【问题讨论】:
-
@Naoki Atkins strtok 在您尝试将它与 std::string 类型的对象一起使用时与字符数组一起使用。
-
您可以随时使用 str.c_str() 将其转换为 const char *。
-
@TheMarlboroMan
strtok采用指向char的指针,因为它修改了数组 -
@VTT,对不起,我的意思是通过从 c_str() 复制来创建 char *...
-
@TheMarlboroMan 为什么使用流很糟糕?!
标签: c++ string split istringstream strtol