关闭

chapter16test9

标签: c++
149人阅读 评论(0) 收藏 举报
分类:


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

#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;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55152次
    • 积分:2952
    • 等级:
    • 排名:第12366名
    • 原创:250篇
    • 转载:0篇
    • 译文:1篇
    • 评论:25条
    最新评论