You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The vector::clear() function is used to remove all the elements of the vector container, thus making it size 0. Its time complexity is O(N). All elements are destroyed one by one.
vector::erase()
The vector::erase() function is used to remove elements from a container from the specified position or range. Its time complexity O(N^2) in the worst case as an erase takes linear time.
When to use what?
clear() removes all the elements from a vector container, thus making its size 0. All the elements of the vector are removed using the clear() function. erase() function, on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.
STL中容器的释放:到底如何使用erase()
STL中erase()函数的功能是用来删除容器中元素的,在析构函数中释放容器内内存资源尤其重要。
然而,看似简单的动作,对不同类型的容器,内部缺做了截然不同的事情,而且不同编译器的实现也有差异。
The text was updated successfully, but these errors were encountered: