需要#include<ostream>:
template class std::basic_ostream提供高水平的字符输出操作,支持格式化的和非格式化的输出操作.
template<
class CharT,
class Traits = std::char_traits<CharT>
> class basic_ostream : virtual public std::basic_ios<CharT, Traits>
我们一般看到的:
std::ostream其实是 typedef std::basic_ostream<char> ostream.
std::wostream其实是 typedef std::basic_ostream<w_char> wostream.
构造函数:
public:
explicit basic_ostream( std::basic_streambuf<CharT, Traits>* sb );
protected:
basic_ostream( const basic_ostream& rhs ) = delete;
protected:
basic_ostream( basic_ostream&& rhs );
1,public explicit的构造函数接受一个std::basic_streambuf的指针作为参数.
2,delete删除的拷贝构造函数,也就是说Stream是无法被拷贝的.
3,protected的移动构造函数,也就是说后续继承了该类的其他类可以支持移动比如std::basic_ofstream
std::basic_ostream::operator=
protected:
basic_ostream& operator=( const basic_ostream& rhs ) = delete;
protected:
basic_ostream& operator=( basic_ostream&& rhs );
1,拷贝赋值符为delete的,也就是说是不支持拷贝的.
2,移动赋值运算符为protected的,也就是说后续继承了该类的其他类可以支持移动比如std::basic_ofstream.
std::basic_ostream::put
basic_ostream& put( char_type ch );
把字符ch放到当前output stream里其效果类似 std::cout<< ch;
如果给定的字符类型不符合当前stream的字符集造成输出失败那么当前流会被设置std::ios::badbit.
std::basic_ostream::write
basic_ostream& write( const char_type* s, std::streamsize count );
把s指向的字符数组的前count个字符插入到当前output stream,如果插入失败那么给当前流设置std::ios::badbit.
std::basic_ostream::tellp
pos_type tellp();
返回写入(写入指的是写入stream)位置.
如果当前output stream被设置了std::ios::failbit那么返回-1;
std::basic_ostream::seekp
basic_ostream& seekp( pos_type pos );
basic_ostream& seekp( off_type off, std::ios_base::seekdir dir);
1,设置绝对写入位置。
2,设置相对写入(写入指的是写入stream)位置(相对于我们给定的dir)
但是需要注意的是: stream中的cin, cout, cerr是不支持的. 另外pos_type是一个很复杂的类型并不是简单的long, unsigned long等等.
一般我们使用的时候直接使用: std::ios::pos_type就可以了并不用像Demo里面定义的这么麻烦
Demo:
#include <iostream>
#include <cstdlib>
#include <exception>
#include <streambuf>
#include <sstream>
int main()
{
std::basic_string<char> str;
std::basic_stringstream<char> strStream(str);
std::fpos<std::char_traits<char>::state_type> pos = strStream.tellg();
std::cout << pos << std::endl;
return 0;
}
std::basic_ostream::operator<<
member function部分(用于给当前流设置flags, states以及对数字类型的输出 和 输出其他stream的streambuf):
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );
std::ios_base::seekdir类型:
std::ios::beg 定位到 文件(File)或者stream 开始位置.
std::ios::cur 定位到文件(File)或者stream当前位置.
std::ios::end 定位到文件(File)或者stream的尾部。