English 中文(简体)
我们能否依靠降低能力trick计?
原标题:Can we rely on the reduce-capacity trick?
  • 时间:2011-10-19 22:32:08
  •  标签:
  • c++
  • std

是否实际上保证了以下降低能力陷阱“工作”?

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

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

对我来说,“工作”似乎只是“工作”(因为能力仍然是非零),我可以发现标准中的任何内容,即“参与者”必须在两个[相同]物体之间交换。

同样,关于序列集装箱:

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?

<>up>(课程,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.18条指出:

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.21 says that one of the basic_string default constructor s post conditions is that capacity() returns an unspecified value.
§ 21.4.23 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.82 says that string::swap runs in constant time, which (effectively) requires that the internal pointers are swapped.

就我所知,符合规定的执行可以有<编码>,其中:最大程度(){返回4;,并将所有内部从一个缓冲地带向另一个缓冲地带进行互换。 (主人可以这样做)

显然,这都是用盐类提取的。 取自C++草案的Im,从Feb28, 11, ;我可以找到病媒复印机的规格。 另外,没有找到for的证据与查询证据并不相同。

问题回答

http://www.gotw.ca/gotw/054.htm“rel=“nofollow”http://www.gotw.ca/gotw/054.htm:

Some implementations may choose to round up the capacity slightly to their next larger internal "chunk size," with the result that the capacity actually ends up being slightly larger than the size.

如何界定这项工作,可能完全是执行性的。 与病媒等集装箱不同的是,铺设装置可产生非常不同的执行。

如果扼杀性执行工作采用小规模优化,那么你的能力就会低于某一门槛值。 如果扼杀装置使用印本,则不写字,不作真实复制。

http://www.gotw.ca/gotw/054.htm rel=“nofollow”http://www.gotw.ca/gotw/054.htm, shrink-to-fit and clear-completely are different tricks. 如果意图是完全清楚的,那么,如果用不设建筑的扼杀手段进行抽取,就会取得更好的结果。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签