而不是:
stringstream szBuffer;
szBuffer>>string;
myFunc(string);
我该怎么做:
muFunc(szBuffer.NextString());
我不想创建一个temp var只是为了将它传递给函数。
答案 0 :(得分:0)
如果您想要读取整个字符串:
// .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),因此您可以逐字读取。