#include<iostream>
#include<vector>
#include<cstdlib> // rand()
#include<algorithm> // sort()
#include <assert.h> // assert()
#include<time.h> // clock() CLOCKS_PER_SEC
using namespace std;
void fill_random_int(vector<int>& v, int cnt) {
v.clear();
for (int i = 0; i < cnt; i++) {
v.push_back(rand());
}
}
void test_sort(vector<int>& v) {
sort(v.begin(),v.end());
for (int i = 0; i < v.size() - 1; i++)
assert(v[i] <= v[i + 1]);
}
int main() {
vector<int> v;
fill_random_int(v,1000000);
test_sort(v);
printf("%e\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
8.42秒!(博主奔腾处理器)
本文介绍了一个使用C++进行随机数生成并排序的程序示例。该程序首先生成100万个随机整数,然后使用STL库中的sort()函数进行排序,并通过assert()函数验证排序结果的正确性。最后,程序输出了排序所花费的时间,为8.42秒。
2万+

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



