gpt4 book ai didi

c++ - 如何使 "strtok function"一次处理多个 token 字符串?函数指针能解决这个问题吗?

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:50 25 4
gpt4 key购买 nike

strtok 函数使用静态变量将字符串解析为标记。因此,当完成多个调用时,这会导致冲突。除了使用线程,我还能做以下事情:thx
- 我可以使用函数指针在两个不同的地方分配函数吗?这会使“strtok”中的静态变量分配到两个不同的地方吗?

//breaking up first by Sentence and than by Word.
char phrase[] = "My dog has fleas.\nAnd he gave them to me.";
char del1[] = "\n";
char del2[] = " ";
char *token1;
char *token2;


token1 = strtok( phrase, del1);

while( token1 != NULL )
{
printf("Sentence: %s",token1);

token2 = strtok( token1, del2);
while( token2 != NULL ){
token2 = strtok( NULL, del2);
printf("WORD: %s",token2);
}

token1 = strtok( NULL, del1);
}

最佳答案

使用 strtok_r()(可重入版本)。

关于c++ - 如何使 "strtok function"一次处理多个 token 字符串?函数指针能解决这个问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com