本网站暗示清除向量可能会改变容量:
http://en.cppreference.com/w/cpp/container/vector/clear
许多实现在调用clear()之后不会释放已分配的内存,从而有效地保持向量的容量()不变.
但根据@JamesKanze的说法,这是错误的,标准要求明确不会改变容量.
标准说什么?
我有一些使用这样的矢量的简单函数(伪代码):
void someFunc(void) {
std::vector<std::string> contentVector;
// here are some operations on the vector
// should I call the clear() here or this could be ommited ?
contentVector.clear();
}
Run Code Online (Sandbox Code Playgroud)
我应该调用clear()还是可以省略这个?