1
相反的:如何使用stringstream返回沒有>>的下一個字符串?
stringstream szBuffer;
szBuffer>>string;
myFunc(string);
如何我不喜歡:
muFunc(szBuffer.NextString());
我不想創建一個臨時變種只是將它傳遞給一個函數。
相反的:如何使用stringstream返回沒有>>的下一個字符串?
stringstream szBuffer;
szBuffer>>string;
myFunc(string);
如何我不喜歡:
muFunc(szBuffer.NextString());
我不想創建一個臨時變種只是將它傳遞給一個函數。
如果你想閱讀整個字符串:
// .str() returns a string with the contents of szBuffer
muFunc(szBuffer.str());
// Once you've taken the string out, clear it
szBuffer.str("");
如果要提取下一行(直到下一個\ n字符),使用istream::getline:
// There are better ways to do this, but for the purposes of this
// demonstration we'll assume the lines aren't longer than 255 bytes
char buf[ 256 ];
szBuffer.getline(buf, sizeof(buf));
muFunc(buf);
函數getline ()也可以使用分隔符作爲第二個參數(默認情況下爲\ n),因此您可以逐字讀取它。