简体   繁体   English

是`std :: vector的时间复杂度 <T> :: clear` *真的*没有指定?

[英]Is the time complexity of `std::vector<T>::clear` *really* not specified?

During the proceedings of this question , it came to light that there appear to be no time complexity requirements placed on std::vector<T>::clear by the C++ standard. 这个问题的过程中,发现似乎没有时间复杂性要求放在C ++标准的std::vector<T>::clear

Table 100 under 23.2.3 says: 23.2.3下的表100说:

Destroys all elements in a . 销毁中的所有元素a Invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator. 使引用a元素的所有引用,指针和迭代器无效并且可能使过去的迭代器无效。 post: a.empty() returns true post: a.empty()返回true

And... that's it. 而且......就是这样。 There's no entry for it specifically under 23.3.6, and no explicit indication that the following applies to clear : 在23.3.6下没有明确的条目,并且没有明确指出以下内容适用于clear

[C++11: 23.3.6.1/1]: A vector is a sequence container that supports random access iterators. [C++11: 23.3.6.1/1]:向量是一个支持随机访问迭代器的序列容器。 In addition, it supports (amortized) constant time insert and erase operations at the end; 此外, 它支持(摊销)最后的恒定时间插入和擦除操作; insert and erase in the middle take linear time . 在中间插入和擦除需要线性时间 Storage management is handled automatically, though hints can be given to improve efficiency. 存储管理是自动处理的,但可以提供提示以提高效率。 [..] [..]

So... is this really true? 那么......这是真的吗? Or have I simply missed it? 或者我只是错过了它?

This seems to be an unintended consequence of DR 704 (and the related DR 1301 ) which removed the wording saying clear() was equivalent to erase(begin(), end()) because erase() requires MoveAssignable, which isn't needed when erasing every element. 这似乎是DR 704 (以及相关的DR 1301 )的意外结果,它删除了说明clear()等同于erase(begin(), end())因为erase()需要MoveAssignable,这是不需要的擦除每个元素时 Removing the definition in terms of erase() also removes the complexity requirement. 根据erase()删除定义也消除了复杂性要求。 That can probably be handled editorially; 这可能是编辑性的; I've raised it with the committee. 我和委员会一起提出了这个问题。

NB std::deque::clear() and std::forward_list::clear() are also affected. NB std::deque::clear()std::forward_list::clear()也会受到影响。 std::list::clear() does have a complexity guarantee. std::list::clear()确实有复杂性保证。

Edit: this is now http://cplusplus.github.com/LWG/lwg-active.html#2231 编辑:现在是http://cplusplus.github.com/LWG/lwg-active.html#2231

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

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