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> 標題也會定義幾個插入全域運算子。 如需詳細資訊,請參閱 operator<< (<ostream>)

第 10% 成員函式可確保表單 ostr << endl 的運算式呼叫 endl(ostr),然後傳回 *this。 第二個和第三個函式可確保其他操作工具,例如 hex,類似的行為。 其餘的函式都格式化輸出功能。

函式

basic_ostream<_Elem, _Tr>& operator<<(basic_streambuf<Elem, Tr> *_Strbuf);

擷取 _Strbuf的項目,則為,如果 _Strbuf 不為 null 指標,並插入資料錄。 擷取在檔案結尾停止,或者為,則會擲回例外狀況會重新擲回) 的例外狀況 (。 它也會停止,,而不需要擷取該項目,則為,如果插入失敗。 如果函式未插入項目,或者為,則會擲回例外狀況,函式會 setstate(failbit)。 在任何情況下,函式會傳回 *this

函式

basic_ostream<_Elem, _Tr>& operator<<(bool _Val);

將布林值 (Boolean) 欄位和插入的轉換 _Val 透過呼叫 use_facet<num_put<Elem, OutIt>(getloc)。 (OutIt(rdbuf), *thisgetlocval)。 在此例中, 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), *thisgetlocval)。 在此例中, 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>貼上 (getloc). put(OutIt(rdbuf), *thisgetlocval)。 在此例中, 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;
}

Output

i is cout
21
15
text in streambuf

需求

標題: <ostream>

命名空間: std

請參閱

參考

basic_ostream Class

operator<< (<ostream>)

iostream 程式設計

iostreams 慣例