chapter16test9


测试效果挺好的,时间明显缩短了

#include<iostream>

#include<vector>
#include<list>
#include<ctime>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
srand(time(0));
int const size = 100000;
vector<int>vi0(size);
for (int i = 0; i < size; i++)
vi0[i] = rand();
vector<int>vi(vi0);    //define vi
list<int>li(vi0.begin(), vi0.begin() + size);   //define li


clock_t start0 = clock();
sort(vi.begin(), vi.end());
clock_t end0 = clock();
cout << "For vector<int> time0 :" << (double)(end0 - start0) / CLOCKS_PER_SEC;
cout << endl;
clock_t start1 = clock();
li.sort();
clock_t end1 = clock();
cout << "For list<int> time1 :" << (double)(end1 - start1) / CLOCKS_PER_SEC;
cout << endl;
clock_t start2 = clock();
copy(vi0.begin(), vi0.begin() + size,li.begin());    //redefine li
copy(li.begin(), li.end(), vi.begin());
sort(vi.begin(), vi.end());
copy(vi.begin(), vi.begin() + size,li.begin());
clock_t end2 = clock();
cout << "For list<int> time2 :" << (double)(end2 - start2) / CLOCKS_PER_SEC;
cout << endl;
return 0;
}
阅读更多
文章标签: c++
个人分类: C++学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭