Is `std::vector<primitive>::clear()` a constant time operation?
c++, stl, vector
Solution
I believe the answer is implementation dependent. It takes at most linear time, but some implementations may choose to optimize this.
Per 'Does clearing a vector affect its capacity?', neither MSVC nor G++ decrease the capacity of their vectors, even when `.clear` is called. Looking at the G++ headers, it is evident that `.clear` is constant-time with the default allocator, as long as the elements are scalar (primitive arithmetic types or pointers).
Problem
Calling `clear()` on a vector will call the destructors of whatever is stored in the vector, which is a linear time operation. But is this the case when the vector contains primitive types like `int` or `double`?