StringReserve

保留内存中字符串的指定大小缓冲区。

bool  StringReserve(
   string&    string_var,       // 字符串
   uint       new_capacity      // 存储字符串的缓冲区大小
   );

参数

string_var

[in][out]  用于更改缓冲区大小的字符串。

new_capacity

[in]  字符串所需的缓冲区大小。如果new_capacity大小小于字符串长度,那么当前缓冲区大小不会更改。

返回值

如果执行成功,返回true,否则 - false。若要接收错误代码,应该调用GetLastError()函数。

注意

通常,字符串大小不等于用于存储字符串的缓冲区大小。当创建字符串时,通常为相应的缓冲区分配一个空白区。StringReserve()函数可以管理缓冲区大小以及为以后操作指定最佳大小。

不同于 StringInit(),StringReserve()函数不更改字符串的内容,也不填充字符。

例如:

void OnStart()
  {
   string s;
//--- 不使用StringReserve检查操作速度
   ulong t0=GetMicrosecondCount();
   for(int i=0; i< 1024; i++)
      s+=" "+(string)i;
   ulong msc_no_reserve=GetMicrosecondCount()-t0;
   s=NULL;
//--- 现在,让我们使用StringReserve执行相同操作
   StringReserve(s,1024 * 3);
   t0=GetMicrosecondCount();
   for(int i=0; i< 1024; i++)
      s+=" "+(string)i;
   ulong msc_reserve=GetMicrosecondCount()-t0;
//--- 检查时间
   Print("Test with StringReserve passed for "+(string)msc_reserve+" msc");   
   Print("Test without StringReserve passed for "+(string)msc_no_reserve+" msc");         
/* Result:
     Test with StringReserve passed for 50 msc
     Test without StringReserve passed for 121 msc
*/
  }

另见

StringBufferLenStringSetLengthStringInitStringSetCharacter