我们可以依靠减少能力的把戏吗? [英] Can we rely on the reduce-capacity trick?

查看:97
本文介绍了我们可以依靠减少能力的把戏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在任何地方实际上是保证下面的减少能力的技巧将工作?

Is it actually guaranteed anywhere that the following reduce-capacity trick will "work"?

int main() {
   std::string s = "lololololol";
   s = "";                        // capacity still non-zero

   string(s).swap(s);             // ?
}

保持非零),我在标准中找不到任何东西,说内容必须在两个[这里,相同的]对象之间交换。

It doesn't seem to "work" for me (in that the capacity remains non-zero), and I can't find anything in the standard that says anything more than that the "contents" must be swapped between the two [here, identical] objects.

类似地,对于序列容器:

Similarly, for sequence containers:

int main() {
   vector<int> v { 1,2,3,4,5 };
   v.clear();                   // capacity still non-zero

   vector<int>(v).swap(v);      // ?
}

据我所知,这个技巧用过的;

As far as I'm aware, this "trick" is semi-widely used; perhaps this widespread adoption is misguided?

(当然,在C ++ 11中,我们有 shrink_to_fit

推荐答案

我一直在教授没有保证标准的方法来降低容量。所有方法已经(并且仍然)实现定义。

I've always been taught that there is no guaranteed standard way to lower the capacity. All methods have been (and still are) implementation defined.

§23.2.1\8说:

§ 23.2.1\8 says:


c $ c> a.swap(b),用于容器 a b 数组之外的标准
容器类型应交换 a b
,无需对单个
容器元素调用任何移动,复制或交换操作...

The expression a.swap(b), for containers a and b of a standard container type other than array, shall exchange the values of a and b without invoking any move, copy, or swap operations on the individual container elements...

这保证了向量的内部指针必须交换。

然而,我找不到任何保证新创建的向量容量的东西。

This guarantees that the internal pointers of vectors must be swapped.
However, I cannot find anything that guarantee on the capacity of a newly created vector.

§21.4.2 \1说, basic_string 缺省构造函数的条件之一是 capacity() 返回一个未指定的值。

§21.4.2 \3说basic_string拷贝构造函数的条件之一是 capacity()返回至少与 size()一样大的值。

§21.4.6.8 \2说 string :: swap 在常量时间运行,这(有效地)要求内部指针被交换。

§ 21.4.2\1 says that one of the basic_string default constructor's post conditions is that capacity() returns an unspecified value.
§ 21.4.2\3 says that one of the basic_string copy constructor's post conditions is that capacity() returns a value at least as big as size().
§ 21.4.6.8\2 says that string::swap runs in constant time, which (effectively) requires that the internal pointers are swapped.

,一个符合的实现可以有 string :: max_size(){return 4;} ,因此将所有内部从一个缓冲区交换到另一个缓冲区将是恒定的时间。 (向量不能这样做)

As far as I can tell, a conforming implementation could have string::max_size() { return 4;}, and swapping all internals from one buffer to another would therefore be constant time. (vector can't do that though)

显然,把这一切都用一粒盐。我引用从2011年2月的C ++草案,'11,我找不到矢量的复制构造函数的规范。此外,未找到 的证据与 的证据不一致。

Obviously, take this all with a grain of salt. I'm quoting from the C++ draft from Feb28,'11, and I can't find specifications for the vector's copy constructor. Also, not finding evidence for is not the same as finding evidence against.

这篇关于我们可以依靠减少能力的把戏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆