大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
const int LENGTH = 1000000;
int main()
{
vector<int>vi0;
srand(time(0));
for (int i = 0; i < LENGTH; i++)
{
vi0.push_back(rand() % 1000);
}
vector<int>vi(vi0);
list<int>li(vi0.begin(), vi0.end());
clock_t time = clock();
sort(vi.begin(), vi.end());
time = clock() - time;
cout << "Time used sort by vector.sort(): ";
cout << (double)(time) / CLOCKS_PER_SEC << " second" << endl;
time = clock();
li.sort();
time = clock() - time;
cout << "Time used sort by list.sort(): ";
cout << (double)(time) / CLOCKS_PER_SEC << " second" << endl;
li.assign(vi0.begin(), vi0.end());
time = clock();
vi.assign(li.begin(), li.end());
sort(vi.begin(), vi.end());
li.assign(vi.begin(), vi.end());
time = clock() - time;
cout << "Time used by generic sort(): ";
cout << (double)(time) / CLOCKS_PER_SEC << " second" << endl;
return 0;
}
运行结果:
考查点:
- vector容器
- list容器
- sort排序
- clock_t计时
注意:
- sort要使用头文件< algorithm >
- 就是用空间换时间,用vector排序然后赋值给list
2024年9月19日21:20:34