为什么 C++ strtok() 为我工作吗?
该程序应该通过 cin 接收输入,对其进行标记,然后输出每个输入以向我表明它工作正常。但事实并非如此。
该程序编译时没有错误,并接受输入,但无法输出任何内容。
我做错了什么?
int main(int argc, char* argv[])
{
string input_line;
while(std::cin >> input_line){
char* pch = (char*)malloc( sizeof( char ) *(input_line.length() +1) );
char *p = strtok(pch, " ");
while (p != NULL) {
printf ("Token: %s\n", p);
p = strtok(NULL, " ");
}
}
return 0;
}
我按照这里的代码示例: http://www.cplusplus.com/reference/ clibrary/cstring/strtok/
谢谢。
The program is supposed to receive an input through cin, tokenize it, and then output each one to show me that it worked properly. It did not.
The program compiles with no errors, and takes an input, but fails to output anything.
What am I doing wrong?
int main(int argc, char* argv[])
{
string input_line;
while(std::cin >> input_line){
char* pch = (char*)malloc( sizeof( char ) *(input_line.length() +1) );
char *p = strtok(pch, " ");
while (p != NULL) {
printf ("Token: %s\n", p);
p = strtok(NULL, " ");
}
}
return 0;
}
I followed the code example here: http://www.cplusplus.com/reference/clibrary/cstring/strtok/
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来您忘记将
input_line
的内容复制到 pch:但我不确定您为什么要进行字符串标记化。正在做
cin>> input_line
不会读取一行,而是读取一个令牌..所以你无论如何都会得到令牌?Looks like you forget to copy the contents of
input_line
to pch:But I'm not sure why you're doing string tokenization anyway. Doing
cin >> input_line
will not read a line, but a token.. so you get tokens anyway?这更像是一个正确性的帖子,汉斯有你的问题。
获取一行输入的正确方法是使用
getline
:std::cin
无论如何都会在空格处中断,因此如果您输入asd 123
并运行您的代码,input_line
首先会是“asd”,然后在循环中第二次是“123”(无需等待输入)。也就是说,获取结果的一种简单方法是使用
stringstream
。每当您显式分配内存时,尤其是使用malloc
时,您可能会遇到困难。这是标记字符串的一种可能的解决方案:如果您确实想使用
strtok
,您可能会这样做:记住,手动内存管理是不好的。对数组使用
向量
,可以避免泄漏。 (你的代码有!)This is more of a correctness post, Hans has your problem.
The correct way to get a line of input is with
getline
:std::cin
breaks at whitespace anyway, so if you typedasd 123
and ran your code,input_line
would first be "asd", then the second time in the loop "123" (without waiting for enter).That said, an easy way to get your result is with a
stringstream
. Any time you explicitly allocate memory, especially withmalloc
, you're probably doing something the hard way. Here's one possible solution to tokenizing a string:If you really want to use
strtok
, you might do something like this:Remember, manually memory management is bad. Use a
vector
for arrays, and you avoid leaks. (Which your code has!)您没有初始化您的字符串。 插入。
在
malloc
行之后You didn't initialize your string. Insert
after the
malloc
line.GMan 的答案可能更好,更纯粹的 c++。这更像是专门使用
strtok()
的混合,因为我认为这是您的目标。我使用了
strdup()
/free()
因为这是复制字符串的最简单方法。在这个问题中,您正在泄漏内存,因为您的malloc()
没有匹配的free()
。也算符>>字符串会在空格处中断,因此不适合获取行。请改用
getline()
。token.cpp
当您运行此程序时,您会得到看似正确的结果/
GMan's answer is probably better and more purely c++. This is more of a mix which specifically uses
strtok()
, since I think that was your goal.I used
strdup()
/free()
since it was the easiest way to copy the string. In the question you were leaking memory since you'dmalloc()
with no matchingfree()
.Also operator>> with the string will break on whitespace and so inappropriate for getting lines. Use
getline()
instead.token.cpp
When you run this, you get what appears to be the correct result/
或者使用这个:
Or use this: