public member function
<locale>

std::time_get::get_time

iter_type get_time (iter_type s, iter_type end, ios_base& str,                    ios_base::iostate& err, tm* t) const;
讀取時間
解析 send 之間的字元序列以表示時間,並將其值儲存到 t 指向的 tm 物件中。

該函式讀取字元,直到讀到的字元無法構成有效的時間序列表達式或 end 已被達到。函式返回的迭代器指向最後一個被處理的字元之後的下一個字元。

如果成功,該函式將設定 tm 結構體 t 的相關成員。其餘成員將保持不變。

在內部,該函式僅呼叫虛保護成員 do_get_time,該成員預設解析與 "%X"strftime 中生成的格式相同的字元。

必要時,該函式會更新 err 以反映操作結果。
- 如果序列格式無效,函式將設定 failbit
- 否則,行為是未定義的,儘管某些實現會將 err 設定為 eofbit(如果函式耗盡了字元序列,即達到了end,無論成功還是失敗),或者設定為 goodbit
在內部,該函式僅呼叫虛保護成員 do_get_time,該成員預設解析與 "%H:%M:%S"strftime 中生成的格式相同的字元。

必要時,該函式會更新 err 以反映操作結果。
- 如果序列格式無效,函式將設定 failbit
- 如果函式在操作過程中耗盡了字元序列(即達到了end),它將設定 eofbit(單個操作可能同時設定 failbiteofbit)。
- 否則,行為是未定義的,儘管某些實現會將 err 設定為 goodbit

引數

s, end
指向序列開始和結束字元的迭代器。使用的範圍是 [s,end),它包含 send 之間的所有字元,包括 s 指向的字元,但不包括 end 指向的字元。
成員型別 iter_type 是此 facet 的迭代器型別(定義為 time_get 的第二個模板引數 InputIterator 的別名)。預設情況下,這是 istreambuf_iterator,允許從 basic_istream 物件隱式轉換。
str
派生自 ios_base 的類物件(通常是輸入流物件)。它僅用於獲取格式資訊。
err
流錯誤狀態物件,型別為 ios_base::iostate,用於儲存錯誤狀態。
t
指向 struct tm (在標頭檔案 <ctime> 中定義)的指標,其成員在成功呼叫此成員函式後會被設定。

返回值

在操作之後,序列中緊隨最後一個被使用字元的下一個字元。
成員型別 iter_type 是此 facet 的迭代器型別(定義為 time_get 的第二個模板引數 InputIterator 的別名)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// time_get::get_time example
#include <iostream>       // std::cout, std::ios
#include <sstream>        // std::istringstream
#include <ctime>          // std::tm
#include <locale>         // std::locale, std::time_get, std::use_facet

int main ()
{
  std::locale loc;        // classic "C" locale

  // get time_get facet:
  const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);

  std::ios::iostate state;
  std::istringstream iss ("07:30:00");
  std::tm when;

  tmget.get_time (iss, std::time_get<char>::iter_type(), iss, state, &when);

  std::cout << "hours: " << when.tm_hour << '\n';
  std::cout << "min: " << when.tm_min << '\n';
  std::cout << "sec: " << when.tm_sec << '\n';
  return 0;
}

輸出

hours: 7
min: 30
sec: 0


資料競爭

訪問該物件以及 send 之間的整個範圍。
引數 strerrt 可能會被修改。

異常安全

如果丟擲異常,facet 物件不會發生任何變化,儘管某些引數可能已經受到影響。

另見