我需要逐步构建一个字符串,并试图找到最好的方法来做到这一点。它可以长到最大为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