c++ - 是 std::vector::reserve(0);合法的?

标签 c++ c++11 vector stl

std::vector::reserve(0); 合法吗?它会做什么?

最佳答案

没有什么可以禁止的。 reserve的效果是:

After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise. Reallocation happens at this point if and only if the current capacity is less than the argument of reserve().1

因为 capacity() 的值永远不会小于 0(它是无符号的),所以这永远不会有任何效果;它永远不会导致重新分配。


<支持> 1. c++ 标准,[vector.capacity]

关于c++ - 是 std::vector::reserve(0);合法的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807356/

相关文章:

c++ - 在 C++ 中填充并返回读取的数据

java - 通过 vector 相加

c++ - 指向 std::vector 的指针,指针声明

c++ - 自注册全局对象

android 矢量可绘制动画不起作用

c++ - 无法访问私有(private)成员:std::copy

java - 在 java 的 jni 中使用非静态对象

c++ - std::invoke(C++1z) 如何工作?

c++ - 提升日期/时间 microsec_clock 编译不正确

c++ - 为什么 CMake 似乎不使用 add_compile_options 命令应用 -pthread ?