|
|
|
|
|
|
t****d 发帖数: 8 | 1 看到网上一个解释:
void reserve(uint res_arg = 0)
change the capacity of a String to the maximum of res_arg and size(). This
may be an increase or a decrease in the capacity.
这个对吗? 我做了个简单的测试,跟踪 i 的值,但是在reserve前后没有变化,哪位
指点一下,谢谢了。
std::string str1("abcdefgh");
int i=str1.size(); // 结果是8
i=str1.capacity(); // 结果是15
str1.reserve(4);
i=str1.capacity(); // 结果是15
i=str1.size(); // 结果是8 | t****t 发帖数: 6806 | 2 official document is as follows:
void reserve(size_type res_arg=0);
1 The member function reserve() is a directive that informs a
basic_string object of a planned change in size, so that it can manage
the storage allocation accordingly.
Effects:
After reserve(), capacity() is greater or equal to the argument of
reserve. [Note: Calling reserve() with a res_arg argument less than
capacity() is in effect a non-binding shrink request. A call with
res_arg <=
【在 t****d 的大作中提到】 : 看到网上一个解释: : void reserve(uint res_arg = 0) : change the capacity of a String to the maximum of res_arg and size(). This : may be an increase or a decrease in the capacity. : 这个对吗? 我做了个简单的测试,跟踪 i 的值,但是在reserve前后没有变化,哪位 : 指点一下,谢谢了。 : std::string str1("abcdefgh"); : int i=str1.size(); // 结果是8 : i=str1.capacity(); // 结果是15 : str1.reserve(4);
|
|
|
|
|
|