Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 345
Lightness Races in Orbit Avatar asked Dec 30 '12 20:12

Lightness Races in Orbit


1 Answers

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.

N.B. 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

like image 142
Jonathan Wakely Avatar answered Oct 16 '22 08:10

Jonathan Wakely