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>)

第一个成员函数可以确保窗体 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);

将布尔型字段以及插入的转换 _Val 通过调用 use_facet<num_put<Elem, OutIt>(getloc)。 放置(OutIt(rdbuf), *thisgetlocval)。 在这里,OutIt 被定义为**<Elem, Tr>**。ostreambuf_iterator 函数返回 *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)rdbuf(.put(OutIt),*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 类

operator<< (<ostream>)

iostream 编程

iostreams 约定