我需要逐步構建一個字符串,並試圖找到最好的方法來做到這一點。它可以長到最大爲10K左右,因此正打算做這樣的事情:使用字符串或流?
const unsigned long long configSize = 10240; //Approximation
void makeMyConfig() {
std::string resp;
std::string input;
resp.reserve(configSize);
while (getInput(input)) {
resp += input;
}
if (resp.length > configSize)
log << "May need to adjust configSize. Validate" << endl;
if (!sendConfig(resp)){
log << "Error sending config" << endl;
}
}
getInput
可以從文件/ TCP康恩或ftp閱讀並在運行時決定的。它接收到const char *並將它放入一個字符串中(我可以避免但爲了方便而離開它)
但是,我聽說有一種處理字符串流的高效方法,但不知道該怎麼做。欣賞任何見解。
不要忘記你的'length'調用的括號:'if(resp.length()> configSize)' – 2011-05-05 13:47:41