gpt4 book ai didi

c++ - vector::erase 是否对 vector 中的元素重新排序?

转载 作者:搜寻专家 更新时间:2023-10-31 01:52:56 25 4
gpt4 key购买 nike

我有一个包含 a,b,c,d,e 的 vector vec[2] 是 c,但它会在我删除/删除 c 后自动重新排序吗?我的意思是 vec[2] 在操作后是 d。

最佳答案

逻辑上是的,因为 vector 是元素的动态数组。你删除一个,然后所有跟随的都被移动。

以同样的方式, vector 的总长度将随着您删除元素而减少。

来自 cplusplus.com

This effectively reduces the vector size by the number of elements removed, calling each element's destructor before.

Because vectors keep an array format, erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions, which may not be a method as efficient as erasing in other kinds of sequence containers (deque, list).

关于c++ - vector::erase 是否对 vector 中的元素重新排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11861926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com