我正在打印当前时间,但我的数量与时间无关

I am printing the current time but I am getting a large number unrelated to time

本文关键字:时间 我的 打印      更新时间:2023-10-16

我需要计算出一种时间。这是一项家庭作业,我的老师说<ctime>包括1987年以来的一些功能,可以告诉您从那时起的时间。这是在排序之前和之后运行的,两个值之间的差异是排序时间。但是,我找不到与时间函数有关的任何内容&amp;1987 ...有人知道他在说什么还是有其他方法可以计算排序时间?

int main()
{
    int n;
    vector<int> data;
    time_t t =time(0);
    cout<<"Vector length?: "<<endl;
    cin>>n;
    srand(time(0));
    for (int i=0; i<n; i++)
    {
        data.push_back(rand()%20+1);
    }
    cout<<"Vector: "<<endl;
    for (int i=0; i<n; i++)
    {
        cout<<data[i]<<" "<<endl;
    }
    cout<<"Time: "<<t<<endl;
    insertionSort(data);

    cout<<"***Insertion Sorted Vector*** "<<endl;
    //cout<<"Time taken: "<<i_t2-i_t1<<endl;
    system("Pause");
    return 0;


}

http://www.cplusplus.com/reference/ctime/time/

功能时间

time_t time (time_t* timer);

获得当前时间获取当前日历时间作为类型的值 time_t。

函数返回此值,如果参数不是null 指针,它还将此值设置为计时器指向的对象。

返回的值通常 00:00小时,1970年1月1日UTC(即当前的UNIX时间戳)。 尽管库可能会使用不同的时间表示: 便携式程序不应使用此功能返回的值 直接,但始终依靠对标准其他元素的调用 库将它们翻译成便携式类型(例如Localtime,GMTime 或 difftime )。


/* time example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */
int main ()
{
  time_t timer;
  struct tm y2k = {0};
  double seconds;
  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
  time(&timer);  /* get current time; same as: timer = time(NULL)  */
  seconds = difftime(timer,mktime(&y2k)); 
         //  ^^^^^^^  see difftime here
  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
  return 0;
}