0
vector<string> CategoryWithoutHashTags;
string tester = "#hello junk #world something #cool";
char *pch;
char *str;
str = new char [tester.size()+1];
strcpy(str, tester.c_str());
pch = strtok(str,"#");
while(pch!=NULL)
{
CategoryWithoutHashTags.push_back(pch);
pch=strtok(NULL,"#");
}
cout<<CategoryWithoutHashTags[0]<<endl;
我想写一个程序,它涉及将所有散列标签字存储在一个字符串向量中。上述程序在第一个索引中存储“hello junk”,而不是“hello”。我可以对程序做出什么改变来实现它?使用strtok从输入字符串中获取某些字符串
请如果你要处理的字符串作为不使用'strtok' –
字,请使用空格作为分隔符,而不是'#'。然后看看第一个字符,看看它是否是一个哈希标签。 – Barmar
'strtok'在多线程应用程序中特别危险。 – deepmax