将`std :: vector`做任何我没有要求的事情

fon*_*ini 1 c++ vector capacity

在C++中,如果我初始化a std::vector v(100);并且从不尝试resize()也不尝试reserve(),是否capacity()保证始终保持不变?我想确保没有内存alloc/freeing/realloc/etc出于性能原因.(是的,它会影响性能;我的功能一直被调用,它们必须快速返回).

全部恢复:

std::vector<float> v;
// somehow, `v' is initialized to have 100 elements
void f() { // this function must return _very_ quickly
    /* do some processing, without ever calling v.resize() or v.reserve(), but
       accesing v.size() and v[i] all the time */
    /* it is guaranteed that no system calls (such as memory management)
       will take place here? */
} // no objects on the stack whose destroyers might try to `delete' anything.
Run Code Online (Sandbox Code Playgroud)

Mic*_*urr 5

vector::reserve()C++ 11中的评论23.3.6.3"向量容量":

保证在调用之后发生的插入期间不会发生重新分配,reserve()直到插入将使向量的大小大于值的值为止 capacity().