C++ primer plus第六版课后编程题答案16.9


 

main169.cpp

#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <ctime>
using namespace std;
static const long MAX=10000;//10000000太大了,改了...
void main169()
{
	vector<int> vi0(MAX);
	vector<int> vi(MAX);
	list<int> li(MAX);


	clock_t start0=clock();
	for(int i=0;i<MAX;i++)
	{
		vi0[i]=rand()%100;
		//vi[i]=vi0[i];
		li.push_back(vi0[i]);
	}

	clock_t end0=clock();
	cout<<"vi0 need  "<<(double)(end0-start0)/CLOCKS_PER_SEC<<"  to initialize!  "<<endl;//2.812

	
	clock_t start1=clock();
	sort(vi.begin(),vi.end());//我的电脑是0.661
	clock_t end1=clock();
	cout<<"vi need  "<<(double)(end1-start1)/CLOCKS_PER_SEC<<"  to sort!  "<<endl;

	
	
	clock_t start2=clock();
	//sort(li.begin(),li.end());//list的排序
	li.sort();//list的排序调用是这样子的
	clock_t end2=clock();//一开始数组太大,半天没排好
	cout<<"li need  "<<(double)(end2-start2)/CLOCKS_PER_SEC<<"  to sort  "<<endl;
	//改MAX之后,分别为 0.028  0.006  1.332

	
	clock_t start3=clock();
	//copy(li.begin(),li.end(),vi.begin());//不能这样,vi.begin()不是输出迭代器
	vector<int> vi1(li.begin(),li.end());
	sort(vi1.begin(),vi1.end());
	copy(vi1.begin(),vi1.end(),li.begin());
	clock_t end3=clock();//一开始数组太大,半天没排好
	cout<<"after this  li need  "<<(double)(end3-start3)/CLOCKS_PER_SEC<<"  to sort  !"<<endl;


	cin.get();





}

 

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注