ostreambuf_iterator 類別
類別範本ostreambuf_iterator描述輸出反覆運算器物件,該物件會使用擷取 運算子 >> 將後續字元元素寫入輸出資料流程。 ostreambuf_iterator
與 ostream_iterator 類別的不同處在於字元 (而非泛型類型) 做為要插入至輸出資料流的物件類型。
語法
template <class CharType = char class Traits = char_traits <CharType>>
參數
CharType
類型,表示 ostreambuf_iterator 的字元類型。 這個引數是選擇性的,而且預設值是 char
。
特性
類型,表示 ostreambuf_iterator 的字元類型。 這個引數是選擇性的,預設值為 char_traits
< CharType > 。
備註
ostreambuf_iterator 類別必須符合輸出迭代器的需求。 使用 ostreambuf_iterator
,演算法可以直接寫入輸出資料流。 此類別提供低階資料流迭代器,允許存取字元格式的原始 (未格式化) I/O 資料流,也能夠略過與進階資料流迭代器相關聯的緩衝和字元轉譯。
建構函式
建構函式 | 描述 |
---|---|
ostreambuf_iterator | 建構 ostreambuf_iterator ,初始化以將字元寫入輸出資料流中。 |
Typedefs
類型名稱 | 描述 |
---|---|
char_type | 類型,提供 ostreambuf_iterator 的字元類型。 |
ostream_type | 類型,提供 ostream_iterator 的資料流類型。 |
streambuf_type | 類型,提供 ostreambuf_iterator 的資料流類型。 |
traits_type | 類型,提供 ostream_iterator 的字元特性類型。 |
成員函式
成員函數 | 描述 |
---|---|
failed | 測試插入至輸出資料流緩衝區是否失敗。 |
操作員
運算子 | 描述 |
---|---|
operator* | 取值運算子,用來實作輸出迭代器運算式 * i = x 。 |
operator++ | 無作用的遞增運算子,傳回 ostreambuf_iterator ,指向在呼叫作業之前它所定址的相同物件。 |
operator= | 此運算子會將字元插入至相關聯的資料流緩衝區。 |
需求
Header: < iterator>
命名空間:std
ostreambuf_iterator::char_type
類型,提供 ostreambuf_iterator
的字元類型。
typedef CharType char_type;
備註
此類型是範本參數 CharType
的同義字。
範例
// ostreambuf_iterator_char_type.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
typedef ostreambuf_iterator<char>::char_type CHT1;
typedef ostreambuf_iterator<char>::traits_type CHTR1;
// ostreambuf_iterator for stream cout
// with new line delimiter:
ostreambuf_iterator< CHT1, CHTR1> charOutBuf ( cout );
// Standard iterator interface for writing
// elements to the output streambuf:
cout << "The characters written to the output stream\n"
<< " by charOutBuf are: ";
*charOutBuf = 'O';
charOutBuf++;
*charOutBuf = 'U';
charOutBuf++;
*charOutBuf = 'T';
charOutBuf++;
cout << "." << endl;
}
/* Output:
The characters written to the output stream
by charOutBuf are: OUT.
*/
ostreambuf_iterator::failed
測試插入至輸出資料流緩衝區是否失敗。
bool failed() const throw();
傳回值
true
如果先前未插入輸出資料流程緩衝區,則為 ;否則 false
為 。
備註
成員函式會在任何先前使用成員 時傳回 true
subf _- >sputc
傳回 eof 的呼叫 。 operator=
範例
// ostreambuf_iterator_failed.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
// ostreambuf_iterator for stream cout
ostreambuf_iterator<char> charOut ( cout );
*charOut = 'a';
charOut ++;
*charOut = 'b';
charOut ++;
*charOut = 'c';
cout << " are characters output individually." << endl;
bool b1 = charOut.failed ( );
if (b1)
cout << "At least one insertion failed." << endl;
else
cout << "No insertions failed." << endl;
}
/* Output:
abc are characters output individually.
No insertions failed.
*/
ostreambuf_iterator::operator*
用來實作輸出反覆運算器運算式 * i = x 的非函數取值運算子。
ostreambuf_iterator<CharType, Traits>& operator*();
傳回值
ostreambuf 迭代器物件。
備註
此運算子只會在輸出反覆運算器運算式 * i = x 中運作,以將字元輸出至資料流程緩衝區。 套用至 ostreambuf 反覆運算器,它會傳回反覆運算器; *iter 傳 回反覆運算器 ,
範例
// ostreambuf_iterator_op_deref.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
// ostreambuf_iterator for stream cout
// with new line delimiter
ostreambuf_iterator<char> charOutBuf ( cout );
// Standard iterator interface for writing
// elements to the output stream
cout << "Elements written to output stream:" << endl;
*charOutBuf = 'O';
charOutBuf++; // no effect on iterator position
*charOutBuf = 'U';
*charOutBuf = 'T';
}
/* Output:
Elements written to output stream:
OUT
*/
ostreambuf_iterator::operator++
無作用的遞增運算子,傳回 ostream 迭代器,指向在呼叫作業之前它所定址的相同字元。
ostreambuf_iterator<CharType, Traits>& operator++();
ostreambuf_iterator<CharType, Traits>& operator++(int);
傳回值
原本定址之字元的參考,或可轉換成 ostreambuf_iterator
< CharType 、 Traits > 的實作定義物件。
備註
運算子是用來實作輸出反覆運算器運算式 * i = x。
範例
// ostreambuf_iterator_op_incr.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
// ostreambuf_iterator for stream cout
// with new line delimiter
ostreambuf_iterator<char> charOutBuf ( cout );
// Standard iterator interface for writing
// elements to the output stream
cout << "Elements written to output stream:" << endl;
*charOutBuf = 'O';
charOutBuf++; // No effect on iterator position
*charOutBuf = 'U';
*charOutBuf = 'T';
}
/* Output:
Elements written to output stream:
OUT
*/
ostreambuf_iterator::operator=
此運算子會將字元插入至相關聯的資料流緩衝區。
ostreambuf_iterator<CharType, Traits>& operator=(CharType _Char);
參數
_字元
要插入至資料流緩衝區的字元。
傳回值
插入至資料流緩衝區的字元參考。
備註
指派運算子,用來實作輸出反覆運算器運算式 * i = x 以寫入輸出資料流程。
範例
// ostreambuf_iterator_op_assign.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
// ostreambuf_iterator for stream cout
// with new line delimiter
ostreambuf_iterator<char> charOutBuf ( cout );
// Standard iterator interface for writing
// elements to the output stream
cout << "Elements written to output stream:" << endl;
*charOutBuf = 'O';
charOutBuf++; // No effect on iterator position
*charOutBuf = 'U';
*charOutBuf = 'T';
}
/* Output:
Elements written to output stream:
OUT
*/
ostreambuf_iterator::ostreambuf_iterator
建構 ostreambuf_iterator
,初始化以將字元寫入輸出資料流中。
ostreambuf_iterator(streambuf_type* strbuf) throw();
ostreambuf_iterator(ostream_type& Ostr) throw();
參數
strbuf
用於初始化輸出資料流緩衝區指標的輸出 streambuf 物件。
Ostr
用於初始化輸出資料流緩衝區指標的輸出資料流物件。
備註
第一個建構函式會使用 strbuf 初始化輸出資料流程緩衝區指標。
第二個建構函式會使用 Ostr
初始化輸出資料流緩衝區指標。 rdbuf
. 儲存的指標不能是 null 指標。
範例
// ostreambuf_iteratorOstreambuf_iterator.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
// ostreambuf_iterator for stream cout
ostreambuf_iterator<char> charOut ( cout );
*charOut = 'O';
charOut ++;
*charOut = 'U';
charOut ++;
*charOut = 'T';
cout << " are characters output individually." << endl;
ostreambuf_iterator<char> strOut ( cout );
string str = "These characters are being written to the output stream.\n ";
copy ( str.begin ( ), str. end ( ), strOut );
}
/* Output:
OUT are characters output individually.
These characters are being written to the output stream.
*/
ostreambuf_iterator::ostream_type
類型,提供 ostream_iterator
的資料流類型。
typedef basicOstream<CharType, Traits> ostream_type;
備註
這個類型與 basicOstream
<CharType, Traits> 同義
範例
如需如何宣告及使用 ostream_type
的範例,請參閱 ostreambuf_iterator。
ostreambuf_iterator::streambuf_type
類型,提供 ostreambuf_iterator
的資料流類型。
typedef basic_streambuf<CharType, Traits> streambuf_type;
備註
此類型與 basic_streambuf
< CharType 、 Traits > 同義,這是 I/O 緩衝區的資料流程類別,在特製化為字元類型 char
時會變成 。 streambuf
範例
如需如何宣告及使用 streambuf_type
的範例,請參閱 ostreambuf_iterator。
ostreambuf_iterator::traits_type
類型,提供 ostream_iterator
的字元特性類型。
typedef Traits traits_type;
備註
此類型是範本參數 Traits
的同義字。
範例
// ostreambuf_iterator_traits_type.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
using namespace std;
typedef ostreambuf_iterator<char>::char_type CHT1;
typedef ostreambuf_iterator<char>::traits_type CHTR1;
// ostreambuf_iterator for stream cout
// with new line delimiter:
ostreambuf_iterator< CHT1, CHTR1> charOutBuf ( cout );
// Standard iterator interface for writing
// elements to the output streambuf:
cout << "The characters written to the output stream\n"
<< " by charOutBuf are: ";
*charOutBuf = 'O';
charOutBuf++;
*charOutBuf = 'U';
charOutBuf++;
*charOutBuf = 'T';
charOutBuf++;
cout << "." << endl;
}
/* Output:
The characters written to the output stream
by charOutBuf are: OUT.
*/
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應