程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++11中的便利工具

C++11中的便利工具

編輯:關於C++

chrono is the name of a header, but also of a sub-namespace: All the elements in this header (except for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std::chrono namespace.

The elements in this header deal with time. This is done mainly by means of three concepts:

Durations
They measure time spans, like: one minute, two hours, or ten milliseconds.
In this library, they are represented with objects of the duration class template, that couples a count representation and a period precision (e.g., ten milliseconds has ten as count representation and milliseconds as period precision).

Time points
A reference to a specific point in time, like one’s birthday, today’s dawn, or when the next train passes.
In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock).

Clocks
A framework that relates a time point to real physical time.
The library provides at least three clocks that provide means to express the current time as a time_point: system_clock, steady_clock and high_resolution_clock.

記錄時長的duration
duration表示一段時間間隔,用來記錄時間長度,可以表示幾秒、幾分鐘或者幾個小時的時間間隔。
原型:

template  >
class duration;

作用:
A duration object expresses a time span by means of a count and a period.

參數:
Rep
An arithmetic type, or a class emulating an arithmetic type, to be used as the type for the internal count.
Period
A ratio type that represents the period in seconds.

#include 
#include 
#include 

int main ()
{
  typedef std::chrono::duration seconds_type;
  typedef std::chrono::duration milliseconds_type;
  typedef std::chrono::duration> hours_type;

  hours_type h_oneday (24);                  // 24h
  seconds_type s_oneday (60*60*24);          // 86400s
  milliseconds_type ms_oneday (s_oneday);    // 86400000ms

  seconds_type s_onehour (60*60);            // 3600s
//hours_type h_onehour (s_onehour);          // NOT VALID (type truncates), use:
  hours_type h_onehour (std::chrono::duration_cast(s_onehour));
  milliseconds_type ms_onehour (s_onehour);  // 3600000ms (ok, no type truncation)

  std::cout << ms_onehour.count() << "ms in 1h" << std::endl;

  return 0;
}

表示時間點的time point
time point表示一個時間點,用來獲取從它的clock的紀元開始所經過的duration和當前時間。
原型:

template 
  class time_point;
// time_point constructors
#include 
#include 
#include 

int main ()
{
  using namespace std::chrono;

  system_clock::time_point tp_epoch;    // epoch value

  time_point > tp_seconds (duration(1));

  system_clock::time_point tp (tp_seconds);

  std::cout << "1 second since system_clock epoch = ";
  std::cout << tp.time_since_epoch().count();
  std::cout << " system_clock periods." << std::endl;

  // display time_point:
  std::time_t tt = system_clock::to_time_t(tp);
  std::cout << "time_point tp is: " << ctime(&tt);

  return 0;
}

獲取系統時鐘的clock
clocks表示當前的系統時鐘。

system_clock
Clock classes provide access to the current time_point.
Specifically, system_clock is a system-wide realtime clock.

steady_clock
Clock classes provide access to the current time_point.
steady_clock is specifically designed to calculate time intervals.

high_resolution_clock
The members of clock classes provide access to the current time_point.
high_resolution_clock is the clock with the shortest tick period. It may be a synonym for system_clock or steady_clock.

#include 
#include 
#include 
#include 

int main ()
{
  using namespace std::chrono;

  high_resolution_clock::time_point t1 = high_resolution_clock::now();

  std::cout << "printing out 1000 stars...\n";
  for (int i=0; i<1000; ++i) std::cout << "*";
  std::cout << std::endl;

  high_resolution_clock::time_point t2 = high_resolution_clock::now();

  duration time_span = duration_cast>(t2 - t1);

  std::cout << "It took me " << time_span.count() << " seconds.";
  std::cout << std::endl;

  return 0;
}
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved