简体   繁体   English

vector :: clear的复杂性是未指定的吗?

[英]Is the complexity of vector::clear unspecified?

Per a discussion from Is `std::vector<primitive>::clear()` a constant time operation? 根据来自`std :: vector <primitive> :: clear()`的讨论是一个恒定时间操作吗? , it was noted that the C++ standard does not appear to specify the running time of vector::clear . ,有人指出,C ++标准似乎没有指定vector::clear的运行时间。

It specifies the running time of list::clear (linear; §23.3.5.4.5), .clear for both ordered (table 102) and unordered associative containers (table 103) (both linear). 它指定了list::clear (线性;§23.3.5.4.5)的运行时间, .clear用于有序(表102)和无序关联容器(表103)(均为线性)。 However, vector::clear appears to be missing (though other vector members, like .data and .swap appear to have specified complexity). 但是, vector::clear似乎缺失(尽管其他vector成员,如.data.swap似乎具有指定的复杂性)。

Is it really unspecified, or did I miss something? 它真的没有说明,还是我错过了什么?

Is it really unspecified, or did I miss something? 它真的没有说明,还是我错过了什么?

Yes . 是的 At the moment, it is really unspecified. 目前,它确实没有具体说明。

There is an open library issue for this , whose text contains a link to a relevant Q&A on StackOverflow. 有一个开放的库问题 ,其文本包含指向StackOverflow上相关问答的链接。 The answer by Jonathan Wakely to that question clarifies what has been going on. Jonathan Wakely对这个问题的回答澄清了一直在发生的事情。

According to the linked proposal, the complexity requirement of clear() should be made linear for all sequence containers. 根据链接的提议, clear()的复杂性要求应该对所有序列容器都是线性的。 However, one must keep in mind that complexity requirements are just upper bounds . 但是,必须记住,复杂性要求只是上限 Per Paragraph 17.5.1.4/7 of the C++11 Standard: 根据C ++ 11标准的第17.5.1.4/7段:

Complexity requirements specified in the library clauses are upper bounds, and implementations that provide better complexity guarantees satisfy the requirements. 库子句中指定的复杂性要求是上限,提供更好复杂性保证的实现满足要求。

This allows for possible optimizations, but does not mandate them. 允许可能的优化,但不强制它们。

Even when the linked proposal will be accepted, we will not be allowed to assume that clear() has O(1) complexity for sequence containers of non-class elements, even though this seems to be a natural and common optimization strategy (and the answer by dasblinkenlight to this question on SO confirms it). 即使链接的提议被接受,我们也不会认为clear()对于非类元素的序列容器具有O(1)复杂性,即使这似乎是一种自然而常见的优化策略(并且通过dasblinkenlight回答这个关于SO的问题证实了这一点。

Implementations will be allowed to adopt this strategy (per 17.5.1.4/7), but they won't be required to do so, because nowhere in the Standard such a constraint is (nor is it proposed to be) specified. 实施将被允许采用这种策略(按照17.5.1.4/7),但是他们不需要这样做,因为标准中没有任何地方规定这样的约束(也没有建议)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM