C++ fastest way to clear or erase a vector

后端 未结 3 1839
死守一世寂寞
死守一世寂寞 2020-12-25 10:35

I have a code where I routinely fill a vector with between 0 and 5000 elements. I know the maximum never exceeds 5000. Instead of initializing vector multiple times, I would

3条回答
  •  礼貌的吻别
    2020-12-25 11:13

    If your struct has a non-trivial destructor, then that needs to be called for all the elements of the vector regardless of how it is emptied. If your struct only has a trivial destructor, the compiler or the standard library implementation is allowed to optimize away the destruction process and give you a O(1) operation.

提交回复
热议问题