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