// NEW.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
void test()
{
std::vector<int> t(128,0);
for (int i = 0;i<100000;i++){
//std::vector<int> t;
for(int j =0;j<128;j++){
//t.push_back(j);
t.at(j)=j;
}
}
}
void test2()
{
//std::vector<int> t(128,0);
for (int i = 0;i<100000;i++){
std::vector<int> t;
for(int j =0;j<128;j++){
t.push_back(j);
//t.at(j)=j;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t start = clock();
test();
clock_t end = clock();
float t = (float)(end-start)/CLOCKS_PER_SEC;
std::cout<<t;
std::cin>>t;
}
test()预先分配空间,耗时0.73
test2()动态增长,耗时7.3
C++ Primer Fifth Edition 英文彩色带书签 http://download.csdn.net/detail/kingeasternsun/5529053
C++ Primer Plus (6th Edition) 英文原版 彩色带书签http://download.csdn.net/detail/kingeasternsun/5508691