此函數首先通過構造一個類型為 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>頭中定義的類。fmt−time_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()函數
