std::get_time()函数

此函数首先通过构造一个类型为 basic_istream::sentry 的对象来访问输入序列。
然后(如果计算 sentry 对象为 true),它调用 time_get::get(使用流的所选区域设置)来执行提取和解析操作,并相应地调整流的内部状态标志。
最后,它在返回之前销毁 sentry 对象。

它用于从应用中输入流的字符中提取字符,并将它们解析为参数fmt中指定的时间和日期信息。获得的数据存储在tmb指向的struct tm对象。

声明

以下是 std::get_time 函数的声明。

template <class charT>
/*unspecified*/ get_time (struct tm* tmb, const charT* fmt);

参数

  • tmb − 指向struct tm类型的对象的指针,其中存储提取的时间和日期信息。struct tm是在ctime>头中定义的类。

  • fmttime_get::get使用 C字符串作为格式字符串(见 time_get::get)。 charT是c字符串中的字符类型。

示例

在下面的例子中解释 get_time() 函数的用法。

#include <iostream>     
#include <iomanip>      
#include <ctime>        

int main () {
  struct std::tm when;
  std::cout << "Please, enter the time: ";
  std::cin >> std::get_time(&when,"%R");   

  if (std::cin.fail()) std::cout << "Error reading time/n";
  else {
    std::cout << "The time entered is: ";
    std::cout << when.tm_hour << " hours and " << when.tm_min << " minutes/n";
  }

  return 0;
}

上一篇: std::put_money()函数 下一篇: std::put_time()函数