#include<iostream>
using namespace std;
#include<vector>
#include<list>
#include<ctime>
#include<string>
#include<algorithm>
const int NUM = 10000;
int main()
{
srand(time(0));
vector<int> vi0(NUM);
for (int i = 0; i < NUM; i++)
vi0[i] = rand();
vector<int> vi(vi0);
list<int> li(vi0.begin(), vi0.end());
cout << "time of vector<int>: \n";
clock_t vector_start = clock();
sort(vi.begin(), vi.end());
clock_t vector_end = clock();
cout << "time is: " << (double)(vector_end - vector_start) / CLOCKS_PER_SEC << "s.\n";
cout << "time of list<int>: \n";
clock_t list_start = clock();
li.sort();
clock_t list_end = clock();
cout << "time is: " << (double)(list_end - list_start) / CLOCKS_PER_SEC << "s.\n";
cout << "time of swith list to vector: \n";
sort(vi0.begin(), vi0.end());
copy(vi0.begin(), vi0.end(), li.begin());
clock_t list_vector_start = clock();
copy(li.begin(), li.end(), vi.begin());
sort(vi.begin(), vi.end());
copy(vi.begin(), vi.end(), li.begin());
clock_t list_vector_end = clock();
cout << "time is : " << (double)(list_vector_end - list_vector_start) / CLOCKS_PER_SEC << "s.\n";
return 0;
}
十六章第九题
最新推荐文章于 2024-05-23 15:09:43 发布
本文通过C++实现,比较了vector和list两种数据结构在排序操作上的性能差异,并进一步探讨了将list转换为vector所需的时间开销。
2223

被折叠的 条评论
为什么被折叠?



