C++11:std::vector::shrink_to_fit 复杂度

标签 c++ c++11 stl time-complexity

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 be MoveInsertable into *this.

Complexity: Linear in the size of the sequence.

关于C++11:std::vector::shrink_to_fit 复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16518533/

相关文章:

c++ - 如何编写 Qt 正则表达式来突出显示以非单词字符开头和结尾的字符串?

.net - CreateEnhMetaFile 和 PlayEnhMetaFile 的 .NET 等价物是什么?

c# - 如何从 C# 调用 C++ native 属性

c++ - 无法将 ‘std::string {aka std::basic_string<char>}’ 转换为 ‘char’ 作为返回

c++ - 为什么会超出内存限制?

c++ - 有人可以解释这个 unique_ptr 代码发生了什么吗?

c++ - std::unordered_set迭代器遍历的复杂度

c++ - std::queue 是否具有事件机制(std::queue 中的信号)

c++ - C++中 vector 数组的最佳库

c++ - 使映射键根据插入顺序排序