最佳答案
使用默认构造函数创建的 std::vector的 capacity()是多少?我知道 size()是零。我们是否可以声明默认构造的向量不调用堆内存分配?
通过这种方式,可以使用单个分配(如 std::vector<int> iv; iv.reserve(2345);)创建具有任意预留的数组。这么说吧,出于某种原因,我不想在2345上启动 size()。
For example, on Linux (g++ 4.4.5, kernel 2.6.32 amd64)
#include <iostream>
#include <vector>
int main()
{
using namespace std;
cout << vector<int>().capacity() << "," << vector<int>(10).capacity() << endl;
return 0;
}
printed 0,10. Is it a rule, or is it STL vendor dependent?