public member function
<locale>

std::time_put::put

iter_type put (iter_type s, ios_base& str, char_type fill, const tm* t,               const char_type* pattern, const char_type* pat_end) const;iter_type put (iter_type s, ios_base& str, char_type fill, const tm* t,               char format, char modifier = 0) const;
寫出時間和日期
將指向 tm 結構的 t 的時間值格式化為字元序列。

該函式將格式化操作產生的字元寫入由 s 指向的序列的第一個字元。

函式返回一個指向輸出序列中最後一個寫入元素之後一個位置的迭代器。

(1) 單個格式說明符
此版本簡單地呼叫虛保護成員 do_put,該成員預設產生的序列與使用由百分號('%')後跟引數 format 組成的字串(當 modifier 不為零時可選地插入)透過 strftime 產生的序列相同。
(2) 格式字串
此版本順序寫入範圍 [fmt_begin,fmt_end) 中的字元,並將任何 格式說明符(如同被 C 函式 strftime 識別的那樣)替換為透過呼叫虛保護成員 do_put 並傳入適當的引數(將 說明符 和可選的 修飾符 作為最終引數)的結果。

有關說明符的更多詳細資訊,請參閱 strftime 的參考。

該函式使用 str 的區域設定的 ctype 方面將序列中的字元(如有必要)窄化char

引數

s
指向輸出序列第一個字元的迭代器。
該序列應足夠大以容納整個表示式。
成員型別 iter_type 是方面(facet)的迭代器型別(定義為 time_put 的第二個模板引數 OutputIterator 的別名)。預設情況下,這是一個 ostreambuf_iterator,允許從 basic_ostream 物件隱式轉換。
str
派生自 ios_base 的類的物件(通常是 *輸出流物件*)。它僅用於獲取格式化資訊(此函式不會向此流寫入任何內容)。
fill
*填充字元*。當格式需要字元填充時,*填充字元* 在輸出插入操作中用於填充空格。
成員型別 char_type 是方面(facet)的字元型別(定義為 time_put 的第一個模板引數 charT 的別名)。
t
指向型別為 struct tm 的物件的指標(在標頭檔案 <ctime> 中定義),其資料將被格式化。
pattern, pat_end
指向模式序列(*格式字串*)的開始和結束字元的指標。
這些字元將被寫入到 s 中,除了 *格式標籤*(如 strftime 所識別的)之外,它們將在被寫入之前替換為它們對應的日期時間表達式。
使用的範圍是 [pattern,pat_end),它包含 patternpat_end 之間的所有字元,包括 pattern 指向的字元,但不包括 pat_end 指向的字元。
請注意,*空字元*(如果存在)也會被寫入,並且函式在找到它們時不會停止,而是繼續處理它們。
成員型別 char_type 是方面(facet)的字元型別(定義為 time_put 的第一個模板引數 charT 的別名)。
format
單個格式說明符;對於 do_put 的預設實現,這應該是 strftime 接受的說明符之一。
modifier
某些實現允許對說明符進行格式修飾。
值為 0'\0')表示沒有修飾符。

返回值

最後寫入字元之後的序列中的下一個字元。
成員型別 iter_type 是方面(facet)的迭代器型別(定義為 time_put 的第二個模板引數 OutputIterator 的別名)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// time_put example
// time_put::put example
#include <iostream>       // std::cout
#include <string>         // std::string
#include <ctime>          // std::time, std::localtime, std::tm, std::time_t
#include <locale>         // std::locale, std::time_put, std::use_facet

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

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

  std::time_t timestamp;
  std::time ( &timestamp );
  std::tm * now = std::localtime ( &timestamp );

  // using pattern string:
  std::string pattern ("Now it's: %I:%M%p\n");
  tmput.put (std::cout, std::cout, ' ', now, pattern.data(), pattern.data()+pattern.length());

  // using single specifier:
  std::cout << "Now it's: ";
  tmput.put (std::cout, std::cout, ' ', now, 'X');
  std::cout << '\n';
  return 0;
}

輸出

Now it's: 12:29PM
Now it's: 12:29:08


資料競爭

會訪問方面物件、t 指向的物件以及 patternpat_end 之間的範圍。
指向 s 的序列將被修改。

異常安全

如果丟擲異常,*方面物件* 不會有任何更改,儘管某些引數可能已被影響。

另見