article at cppreference.com告诉 std::vector::shrink_to_fit
的复杂度是恒定的。据我所知,只有在元素不移动的情况下才有可能,因为如果它们移动了,那么复杂度就是 n。它还说所有迭代器,包括过去的结束迭代器,都可能无效。这意味着元素的移动是一种明确定义的可能性。
文章有问题吗? ... 还是有什么我不知道的魔法?
最佳答案
文章是有问题,我修好了。
虽然官方标准没有说明 std::vector::shrink_to_fit
的复杂性,但他们在 N3376 中更改了措辞,从而修复了 DR 2033:
23.3.6.3:
void shrink_to_fit();
Requires:
T
shall beMoveInsertable
into*this
.Complexity: Linear in the size of the sequence.
关于C++11:std::vector::shrink_to_fit 复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16518533/