basic_ostream 類別
這個類別範本描述一個物件,該物件控制將元素和編碼物件插入數據流緩衝區,其類型 Elem
為 ,也稱為 char_type,其字元特性是由 類別 Tr
所決定,也稱為 traits_type。
語法
template <class Elem, class Tr = char_traits<Elem>>
class basic_ostream : virtual public basic_ios<Elem, Tr>
參數
Elem
char_type
。
Tr
字元 traits_type
。
備註
多載 運算子<< 的大部分成員函式都是格式化的輸出函式。 它們遵循下列模式:
iostate state = goodbit;
const sentry ok(*this);
if (ok)
{try
{<convert and insert elements
accumulate flags in state> }
catch (...)
{try
{setstate(badbit);
}
catch (...)
{}
if ((exceptions()& badbit) != 0)
throw; }}
width(0);
// Except for operator<<(Elem)
setstate(state);
return (*this);
其他兩個成員函式是未格式化的輸出函式。 它們遵循下列模式:
iostate state = goodbit;
const sentry ok(*this);
if (!ok)
state |= badbit;
else
{try
{<obtain and insert elements
accumulate flags in state> }
catch (...)
{try
{setstate(badbit);
}
catch (...)
{}
if ((exceptions()& badbit) != 0)
throw; }}
setstate(state);
return (*this);
如果兩組函式在插入元素時發生失敗,則會呼叫 setstate(badbit)。
類別basic_istream Elem 的物件,Tr> 只會儲存類別的虛擬公用基底物件,<basic_ios Elem, Tr>。<
範例
若要深入了解輸出資料流,請參閱 basic_ofstream 類別的範例。
建構函式
建構函式 | 描述 |
---|---|
basic_ostream | 建構 basic_ostream 物件。 |
成員函式
成員函數 | 描述 |
---|---|
flush | 清除緩衝區。 |
put | 將字元置入資料流中。 |
seekp | 重設輸出資料流中的位置。 |
sentry | 此巢狀的類別會描述物件,該物件的宣告會將格式化輸出函式和未格式化輸出函式結構化。 |
swap | 用所提供的 basic_ostream 物件的值交換這個 basic_ostream 物件中的值。 |
tellp | 報告輸出資料流中的位置。 |
write | 將字元置入資料流中。 |
操作員
運算子 | 描述 |
---|---|
operator= | 將提供的 basic_ostream 物件參數值指派為這個物件。 |
運算子<< | 寫入資料流。 |
需求
Header:<ostream>
命名空間:std
basic_ostream::basic_ostream
建構 basic_ostream
物件。
explicit basic_ostream(
basic_streambuf<Elem, Tr>* strbuf,
bool _Isstd = false);
basic_ostream(basic_ostream&& right);
參數
strbuf
basic_streambuf 類型的物件。
_Isstd
若這是標準資料流,即為 true
;否則為 false
。
right
類型為 basic_ostream
之物件的右值參考。
備註
第一個建構函式會藉由呼叫 init(strbuf
) 初始化基底類別。 第二個建構函式會藉由呼叫 basic_ios::move(right)
初始化基底類別。
範例
若要深入了解輸出資料流,請參閱 basic_ofstream::basic_ofstream 的範例。
basic_ostream::flush
清除緩衝區。
basic_ostream<Elem, Tr>& flush();
傳回值
basic_ostream 物件的參考。
備註
如果 rdbuf 不是 Null 指標,函式會呼叫 rdbuf-pubsync>。 如果傳回 -1,函式會呼叫 setstate(badbit)。 它會傳 回 *this。
範例
// basic_ostream_flush.cpp
// compile with: /EHsc
#include <iostream>
int main( )
{
using namespace std;
cout << "test";
cout.flush();
}
test
basic_ostream::operator<<
寫入資料流。
basic_ostream<Elem, Tr>& operator<<(
basic_ostream<Elem, Tr>& (* Pfn)(basic_ostream<Elem, Tr>&));
basic_ostream<Elem, Tr>& operator<<(
ios_base& (* Pfn)(ios_base&));
basic_ostream<Elem, Tr>& operator<<(
basic_ios<Elem, Tr>& (* Pfn)(basic_ios<Elem, Tr>&));
basic_ostream<Elem, Tr>& operator<<(basic_streambuf<Elem, Tr>* strbuf);
basic_ostream<Elem, Tr>& operator<<(bool val);
basic_ostream<Elem, Tr>& operator<<(short val);
basic_ostream<Elem, Tr>& operator<<(unsigned short val);
basic_ostream<Elem, Tr>& operator<<(int __w64 val);
basic_ostream<Elem, Tr>& operator<<(unsigned int __w64 val);
basic_ostream<Elem, Tr>& operator<<(long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long __w64 val);
basic_ostream<Elem, Tr>& operator<<(long long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long long val);
basic_ostream<Elem, Tr>& operator<<(float val);
basic_ostream<Elem, Tr>& operator<<(double val);
basic_ostream<Elem, Tr>& operator<<(long double val);
basic_ostream<Elem, Tr>& operator<<(const void* val);
參數
Pfn
函式指標。
strbuf
stream_buf
物件的指標。
val
要寫入至資料流的元素。
傳回值
basic_ostream 物件的參考。
備註
<ostream> 標頭也會定義數個全域插入運算符。 如需詳細資訊,請參閱運算子<<。
第一個成員函式可確保表單ostr << endl
的運算式會呼叫 endl(ostr),然後傳回 *this。 第二個和第三個函式可確保其他操作工具 (例如 hex) 具有類似的行為。 其餘函式都是格式化的輸出函式。
函式
basic_ostream<Elem, Tr>& operator<<(basic_streambuf<Elem, Tr>* strbuf);
如果 strbuf 不是 Null 指標,則會從 strbuf 擷取元素,並插入它們。 擷取會在檔案結尾處停止,或在擷取擲回例外狀況時停止 (此例外狀況會再次擲回)。 如果插入失敗,它也會停止,而不會擷取有問題的元素。 如果函式未插入任何元素,或是擷取擲回例外狀況,函式會呼叫 setstate(failbit)。 在任何情況下,函式會傳 回 *this。
函式
basic_ostream<Elem, Tr>& operator<<(bool val);
_Val
會轉換成布爾值欄位,並藉由呼叫 use_facet num_put<<Elem, OutIt(
>getloc 來插入它。 put(OutIt(rdbuf), *this, getloc
, val. 在這裡,OutIt
定義為 ostreambuf_iterator<Elem, Tr。> 函式會傳 回 *this。
函式
basic_ostream<Elem, Tr>& operator<<(short val);
basic_ostream<Elem, Tr>& operator<<(unsigned short val);
basic_ostream<Elem, Tr>& operator<<(int val);
basic_ostream<Elem, Tr>& operator<<(unsigned int __w64 val);
basic_ostream<Elem, Tr>& operator<<(long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long val);
basic_ostream<Elem, Tr>& operator<<(long long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long long val);
basic_ostream<Elem, Tr>& operator<<(const void* val);
每個都會將 val 轉換成數值字段,並藉由呼叫 use_facet num_put<<Elem, OutIt>(getloc
) 來插入它。 put(OutIt(rdbuf
), *this, getloc
, val. 在這裡,OutIt 定義為 ostreambuf_iterator Elem, Tr>。< 函式會傳 回 *this。
函式
basic_ostream<Elem, Tr>& operator<<(float val);
basic_ostream<Elem, Tr>& operator<<(double val);
basic_ostream<Elem, Tr>& operator<<(long double val);
每個都會將 val 轉換成數值字段,並藉由呼叫 use_facet num_put<<Elem, OutIt().put(OutIt>(getloc
rdbuf
), *this, getloc
, val) 來插入它。 在這裡,OutIt 定義為 ostreambuf_iterator Elem, Tr>。< 函式會傳 回 *this。
範例
// basic_ostream_op_write.cpp
// compile with: /EHsc
#include <iostream>
#include <string.h>
using namespace std;
ios_base& hex2( ios_base& ib )
{
ib.unsetf( ios_base::dec );
ib.setf( ios_base::hex );
return ib;
}
basic_ostream<char, char_traits<char> >& somefunc(basic_ostream<char, char_traits<char> > &i)
{
if (i == cout)
{
i << "i is cout" << endl;
}
return i;
}
class CTxtStreambuf : public basic_streambuf< char, char_traits< char > >
{
public:
CTxtStreambuf(char *_pszText)
{
pszText = _pszText;
setg(pszText, pszText, pszText + strlen(pszText));
};
char *pszText;
};
int main()
{
cout << somefunc;
cout << 21 << endl;
hex2(cout);
cout << 21 << endl;
CTxtStreambuf f("text in streambuf");
cout << &f << endl;
}
basic_ostream::operator=
將提供的 basic_ostream
物件參數值指派給這個物件。
basic_ostream& operator=(basic_ostream&& right);
參數
right
basic_ostream
物件的 rvalue
參考。
備註
這個成員運算子會呼叫 swap (right)
。
basic_ostream::p ut
將字元置入資料流中。
basic_ostream<Elem, Tr>& put(char_type _Ch);
參數
_Ch
字元。
傳回值
basic_ostream 物件的參考。
備註
未格式化的輸出函式會插入專案 _Ch。 它會傳 回 *this。
範例
// basic_ostream_put.cpp
// compile with: /EHsc
#include <iostream>
int main( )
{
using namespace std;
cout.put( 'v' );
cout << endl;
wcout.put( L'l' );
}
v
l
basic_ostream::seekp
重設輸出資料流中的位置。
basic_ostream<Elem, Tr>& seekp(pos_type _Pos);
basic_ostream<Elem, Tr>& seekp(off_type _Off, ios_base::seekdir _Way);
參數
_Pos
資料流中的位置。
_Off
相對於 _Way的位移。
_道路
其中一個 ios_base::seekdir 列舉。
傳回值
basic_ostream 物件的參考。
備註
如果fail為 false
,則第一個成員函式會針對某些pos_type
暫存物件newpos
呼叫 newpos = rdbuf- > pubseekpos(_Pos)。 如果 fail
為 false,第二個函式會呼叫 newpos = rdbuf- > pubseekoff(_Off,_Way)。 在任一情況下,如果 (off_type
)newpos == (off_type
)(-1) (置放作業失敗),則函式會呼叫 istr.setstate(failbit)。 這兩個函式都會傳回 *this。
範例
// basic_ostream_seekp.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>
int main()
{
using namespace std;
ofstream x("basic_ostream_seekp.txt");
streamoff i = x.tellp();
cout << i << endl;
x << "testing";
i = x.tellp();
cout << i << endl;
x.seekp(2); // Put char in third char position in file
x << " ";
x.seekp(2, ios::end); // Put char two after end of file
x << "z";
}
0
7
basic_ostream::sentry
此巢狀的類別會描述物件,該物件的宣告會將格式化輸出函式和未格式化輸出函式結構化。
class sentry { public: explicit sentry(basic_ostream Elem, Tr>& _Ostr<; operator bool() const; ~sentry(; };
備註
此巢狀的類別會描述物件,該物件的宣告會將格式化輸出函式和未格式化輸出函式結構化。 如果 ostr。好是true
和骨頭。tie 不是 Null 指標,建構函式會呼叫 ostr.tie-flush>。 建構函式接著會將 所 ostr.good
傳回的值儲存在 中 status
。 稍後的 operator bool
呼叫會傳遞這個預存值。
如果 uncaught_exception
傳回 false
和 flags & unitbuf 為非零,解構函式會呼叫 flush。
basic_ostream::swap
用所提供的 basic_ostream
的值交換這個 basic_ostream
物件的值。
void swap(basic_ostream& right);
參數
right
對 basic_ostream
物件的參考。
備註
成員函式會呼叫 basic_ios::swap(right)
,以交換此物件的內容以取得許可權的內容。
basic_ostream::tellp
報告輸出資料流中的位置。
pos_type tellp();
傳回值
輸出資料流中的位置。
備註
如果fail為 false
,則成員函式會傳回 rdbuf-> pubseekoff(0, cur
in) 。 否則會傳回 pos_type
(-1)。
範例
如需 tellp
的使用範例,請參閱 seekp。
basic_ostream::write
將字元置入資料流中。
basic_ostream<Elem, Tr>& write(const char_type* str, streamsize count);
參數
計數
要置入資料流中的字元計數。
str
要置入資料流中的字元。
傳回值
basic_ostream 物件的參考。
備註
未格式化的輸出函式會插入從 str 開始的 count 元素序列。
範例
如需 write
的使用範例,請參閱 streamsize。