题目为:
.h文件
#ifndef TIME_H
#define TIME_H
class Time
{
public:
stopWatch();
void start();
void stop();
int getElapseTime();
private:
int startTime;
int endTime;
};
#endif
.cpp文件
#include "Time.h"
#include <ctime>
#include <cstdlib>
Time::stopWatch()
{
startTime = time(0);
}
void Time::start()
{
startTime = time(0);
}
void Time::stop()
{
endTime = time(0);
}
int Time::getElapseTime()
{
return startTime - endTime;
}
测试文件.cpp
#include <iostream>
#include "Time.h"
using namespace std;
void selectionSort(double list[], int listSize)
{
for(int i = 0; i < listSize; i++)
{
double currentMin = list[i];
int currentMinIndex = i;
for(int j = i + 1; j < listSize; j++)
{
if(currentMin > list[j])
{
currentMin = list[j];
currentMinIndex = j;
}
}
if(currentMinIndex != i)
{
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
int main()
{
const int s = 100000;
int list[s];
for(int i = 1; i < s; i++)
{
list[i] = rand() % s;
}
stopWatch watch;
watch.start();
selectionSort(list, s);
watch.stop();
cout << "Time is " << watch.getElapasedTime() << endl;
return 0;
}
编译并运行后弹出了ctime库源代码,并报错
#pragma GCC system_header
#include <bits/c++config.h>
#include <time.h>
#ifndef _GLIBCXX_CTIME
#define _GLIBCXX_CTIME 1
// Get rid of those macros defined in <time.h> in lieu of real functions.
#undef clock
#undef difftime
#undef mktime
#undef time
#undef asctime
#undef ctime
#undef gmtime
#undef localtime
#undef strftime
namespace std
{
using ::clock_t;
using ::time_t;
using ::tm;
using ::clock;
using ::difftime;
using ::mktime;
using ::time;
using ::asctime;
using ::ctime;
using ::gmtime;
using ::localtime;
using ::strftime;
} // namespace
#endif