【发布时间】:2019-03-25 12:11:13
【问题描述】:
我有一个默认的构造函数,它接受一个字符串类变量(不是char*),并且需要用分隔符标记该字符串,在我的特殊情况下是逗号。由于我使用的是字符串类,因此我无法使用strtok(),因为它需要char* 作为输入而不是字符串类。鉴于下面的代码,鉴于前两个标记是字符串,第三个是 in 和第四个是 double,我如何将字符串拆分为更小的字符串?
private string a;
private string b;
private int x;
private double y;
StrSplit::StrSplit(string s){
a = // tokenize the first delimiter and assign it to a
b = // tokenize the second delimiter and assign it to b
x = // tokenize the third delimiter and assign it to x
y = // tokenize the fourth delimiter and assign it to y
}
【问题讨论】:
-
你能把 s.cstr() 变成 strtok(delimiter,s.cstr()) 吗?可以参考:cplusplus.com/reference/string/string/c_str
-
同时使用#include string 和#include cstring s.cstr() 导致“std::string”没有名为“cstr”的成员”
-
应该是c_str()
-
Maverick 就是这样。我现在唯一遇到的问题是:“警告:不推荐将字符串常量转换为‘char*’[-Wwrite-strings]。”我想解决这个错误,但不知道如何开始。
标签: c++ string split token tokenize