N个数据需要排序,vector+算法sort和list+sort的效率比较

#include 
#include 
using namespace std;
#include 
#include 
#include 

int main()
{
	srand(time(0));
	const int N = 1000;
	vector v;
	v.reserve(N);

	list lt2;

	for (int i = 0; i < N; i++)
	{
		auto e = rand();
		v.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	lt2.sort();
	int end2 = clock();

	cout << "vector sort:" << end1 - begin1 << endl;
	cout << "list sort:" << end2 - begin2 << endl;
}

你可能感兴趣的:(C++,开发语言)