當我使用的strtok來標記一個C++字符串,它發生混亂的問題,請參閱下面的簡單代碼:C++的strtok在函數改變原始字符串值作爲參數
void a(string s){
strtok((char*)s.c_str(), " ");
}
int main(){
string s;
s = "world hello";
a(s);
cout<<s<<endl;
return 0;
}
程序輸出「世界」。 不應該輸出「世界你好」嗎?因爲我將字符串作爲值參數傳遞給函數a,strtok不應該修改原始的字符... 任何人都可以解釋這個技巧。 謝謝。
不要這樣做,你要求的內存腐敗問題。 是的,strtok會改變它的輸入。 http://www.cplusplus.com/reference/clibrary/cstring/strtok/ –