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

注解

重载 operator<< 的大部分成员函数是格式化的输出函数。 它们遵循以下模式:

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 对象参数的值赋给此对象。
operator<< 写入流。

要求

标头:<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 不是空指针,则函数将调用 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 不是空指针,则从 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>(getloc). put(OutIt(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::put

将字符放入流中。

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 的偏移量。

_Way
其中一个 ios_base::seekdir 枚举。

返回值

对 Basic_ostream 对象的引用。

注解

对于某些 pos_type 临时对象 newpos,如果 failfalse,则第一个成员函数调用 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.goodtrue 且 ostr.tie 不是空指针,则构造函数调用 ostr.tie->flush 然后,构造函数将 ostr.good 返回的值存储在 status 中。 稍后对 operator bool 的调用可提供此存储值。

如果 uncaught_exception 返回 false 并且 flagsunitbuf 为非零,则析构函数调用 flush

basic_ostream::swap

将此 basic_ostream 对象的值与提供的 basic_ostream 的值进行交换。

void swap(basic_ostream& right);

参数

right
basic_ostream 对象的引用。

备注

此成员函数调用 basic_ios::swap(right) 以将此对象的内容与 right 的内容进行交换。

basic_ostream::tellp

报告输出流中的位置。

pos_type tellp();

返回值

输出流中的位置。

备注

如果 failfalse,则成员函数返回 rdbuf->pubseekoff(0, cur, in)。 否则,将返回 pos_type(-1)。

示例

有关使用 tellp 的示例,请参阅 seekp

basic_ostream::write

将字符放入流中。

basic_ostream<Elem, Tr>& write(const char_type* str, streamsize count);

参数

count
要放入流中的字符计数。

str
要放入流中的字符。

返回值

对 Basic_ostream 对象的引用。

备注

未格式化的输出函数插入从 str 开始的 count 元素的序列。

示例

有关使用 write 的示例,请参阅 streamsize

另请参阅

C++ 标准库中的线程安全
iostream 编程
iostreams 约定