简体   繁体   中英

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.

Table 100 under 23.2.3 says:

Destroys all elements in a . Invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator. post: a.empty() returns 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 :

[C++11: 23.3.6.1/1]: A vector is a sequence container that supports random access iterators. 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. Removing the definition in terms of erase() also removes the complexity requirement. 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. std::list::clear() does have a complexity guarantee.

Edit: this is now http://cplusplus.github.com/LWG/lwg-active.html#2231

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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