[英]std::basic_string::reserve() specifics
I was reading the standard on std::basic_string::reserve(size_type res_arg=0)
. 我正在阅读
std::basic_string::reserve(size_type res_arg=0)
。 It says this: 它说:
void reserve(size_type res_arg=0);
The member function
reserve()
is a directive that informs abasic_string
object of a planned change in size, so that it can manage the storage allocation accordingly.成员函数
reserve()
是一个指令,用于通知basic_string
对象计划中的大小更改,以便它可以相应地管理存储分配。Effects: After
reserve()
,capacity()
is greater or equal to the argument of reserve.效果:在
reserve()
,capacity()
等于或大于reserve的参数。 [ Note: Callingreserve()
with a res_arg argument less thancapacity()
is in effect a non-binding shrink request.[ 注意:调用res_arg参数小于Capacity
reserve()
的reservecapacity()
实际上是一个非绑定收缩请求。 A call withres_arg <= size()
is in effect a non-binding shrink-to-fit request.使用
res_arg <= size()
调用res_arg <= size()
是一个非绑定的“缩小以适合”请求。 — end note ]—尾注]
Throws: length_error if
res_arg > max_size()
抛出:如果
res_arg > max_size()
则为res_arg > max_size()
The standard seems to be making a distinction between calling reserve()
where res_arg < capacity()
and calling reserve()
where res_arg <= size()
. 标准似乎是使呼叫区分
reserve()
其中res_arg < capacity()
和调用reserve()
其中res_arg <= size()
res_arg <= size()
is easy to understand, shrink_to_fit()
is called and the implementation is free to do whatever it wants since it's non-binding. res_arg <= size()
很容易理解, shrink_to_fit()
,并且实现是自由执行的,因为它是非绑定的。
But what about cases where res_arg < capacity()
? 但是
res_arg < capacity()
呢? The standard says " a non-binding shrink request " and not " a non-binding shrink-to-fit request ". 该标准说“ 非约束性收缩请求 ”而不是“ 非约束性收缩适应请求 ”。 What is the difference between a shrink-to-fit request and a shrink request?
缩小至匹配请求与缩小请求之间有什么区别? Is this just an unfortunate inconsistency?
这只是不幸的矛盾之处吗?
std::string::shrink_to_fit()
will shrink the capacity()
to the size()
. std::string::shrink_to_fit()
将收缩capacity()
的size()
That is differnt then shrinking the capacity()
to a number less than capacity()
but more than size()
. 那是不同的,然后将Capacity
capacity()
缩小到一个小于Capacity capacity()
但大于size()
。 In effect 有效
std:string foo = "test";
foo.reserve(20); // capaicty:20 size:4
foo.reserve(10); // capaicty:10 size:4
foo.reserve(20); // capaicty:20 size:4
foo.shrink_to_fit(); // capaicty:04 size:4
shrink to fit: Reduce capacity to fit size.
缩小以适合:减小容量以适合尺寸。
First mention on google is for a vector . Google首先提到的是向量 。
Shrink just well, shrinks (capacity down). 缩小得恰到好处,缩小(容量下降)。 If you're reserving less then the current size in essence that would be preparing to reduce the container to fit a shorter string, hence the 'fit'.
如果您保留的空间较小,那么实质上是当前的大小,该大小将准备减少容器以适合较短的字符串,因此称为“适合”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.