Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何编写高效的字符串缓冲区_C++_String_Performance_Buffer - Fatal编程技术网

C++ 如何编写高效的字符串缓冲区

C++ 如何编写高效的字符串缓冲区,c++,string,performance,buffer,C++,String,Performance,Buffer,我需要一种方法来建立一个字符串。我不知道它的大小,而且它一定非常有效。 今天我用这个: std::strstreambuf* sBuf = new std::strstreambuf(20*1024*1024); std::ostream* outS = new std::ostream(sBuf); (*outS) << ... << ... ... CString outputStr = (*outS).str(); std::streambuf*sBuf=新st

我需要一种方法来建立一个字符串。我不知道它的大小,而且它一定非常有效。 今天我用这个:

std::strstreambuf* sBuf = new std::strstreambuf(20*1024*1024);
std::ostream* outS = new std::ostream(sBuf);
(*outS) << ... << ...
... 
CString outputStr = (*outS).str();
std::streambuf*sBuf=新std::streambuf(20*1024*1024);
std::ostream*outS=新std::ostream(sBuf);

(*outS)我假设调用了
溢出
,因为缓冲区中没有足够的空间。现在,当新分配的缓冲区再次只提供一个小的放大时,
overflow
将很快再次被调用


为了克服这种情况,可能需要使用自定义分配器实现一个
std::basic_stringbuf
,该分配器反过来将分配更多空间或更智能地进行分配,例如,如果以前的分配相当大,则分配得更多,等等。“.”

中提供了一个大致的概念。字符串中将包含什么?CString(outputStr)将进行另一次计算。所以-我需要一种使用运算符Yes编写字符串的方法,明白了,但如果它们只是字符串,为什么不简单地将它们与
+
连接起来,然后输出
c_str()
?首先,我希望尽可能少地分配内存,使用+可能会导致一次又一次的重新分配。第二,在。。。在上面的示例中,隐藏了大量代码,这些代码依赖于使用我最初想知道的东西。