std::ostream_iterator
來自 cppreference.com
< cpp | 迭代器 (iterator)
定義於標頭檔案 <iterator> |
||
template< class T, class CharT = char, |
(C++17 前) | |
template< class T, class CharT = char, |
(C++17 起) | |
std::ostream_iterator
是一個單趟 LegacyOutputIterator,它使用 operator<<
將型別為 T
的連續物件寫入為其構造的 std::basic_ostream 物件。在每次寫入操作之後,會將可選的分隔符字串寫入輸出流。當迭代器(無論是否解引用)被賦值時,執行寫入操作。遞增 std::ostream_iterator
是一個空操作。
在典型的實現中,std::ostream_iterator
的唯一資料成員是指向關聯的 std::basic_ostream
的指標和指向分隔符字串中第一個字元的指標。
寫入字元時,std::ostreambuf_iterator 更高效,因為它避免了每個字元構造和析構哨兵物件的開銷。
目錄 |
[編輯] 成員型別
成員型別 | 定義 | ||||
iterator_category(迭代器類別)
|
std::output_iterator_tag | ||||
value_type(值型別)
|
void(空) | ||||
difference_type(差值型別)
|
| ||||
pointer(指標)
|
void(空) | ||||
reference(引用)
|
void(空) | ||||
char_type(字元型別)
|
CharT
| ||||
traits_type(特性型別)
|
特性
| ||||
ostream_type(輸出流型別)
|
std::basic_ostream<CharT, Traits> |
成員型別 |
(C++17 前) |
[編輯] 成員函式
構造新的 ostream_iterator (public member function) | |
析構一個 ostream_iterator (public member function) | |
將一個物件寫入關聯的輸出序列 (public member function) | |
無操作 (public member function) | |
無操作 (public member function) |
[編輯] 示例
執行此程式碼
#include <iostream> #include <iterator> #include <numeric> #include <sstream> int main() { std::istringstream str("0.11 0.22 0.33 0.44"); std::partial_sum(std::istream_iterator<double>(str), std::istream_iterator<double>(), std::ostream_iterator<double>(std::cout, ", ")); std::cout << '\n'; }
輸出
0.11, 0.33, 0.66, 1.1,
[編輯] 另請參閱
寫入std::basic_streambuf的輸出迭代器 (class template) | |
從std::basic_istream讀取的輸入迭代器 (class template) | |
(庫基礎 TS v2) |
一個輸出迭代器,將連續的元素寫入輸出流,並用分隔符分隔相鄰元素。 (類模板) |