一 代碼
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;class TEST{
public:TEST(){std::cout << "construct t" << std::endl;}
};int main()
{std::cout << "hello,world" <<std::endl;std::vector<TEST> vt;vt.reserve(5);int n = vt.size();std::cout << n << std::endl;vt.resize(10);n = vt.size();std::cout << n << std::endl;return 0;
}
// 輸出
hello,world
0
construct t
construct t
construct t
construct t
construct t
construct t
construct t
construct t
construct t
construct t
10
請按任意鍵繼續. . .
二 區別
reserve: 僅改變容量,不改變元素數量和值;不調用構造函數;
resive: 改變容量,改變元素數量,調用構造函數