Notice: 提取 和 读取 在该篇博客里面是完全不同的意思.
template class std::basic_istream提供高水平的格式化输入操作,支持格式化和未格式化的输入。
template<
class CharT,
class Traits = std::char_traits<CharT>
> class basic_istream : virtual public std::basic_ios<CharT, Traits>
我们经常使用的std::cin,std::wcin其实是:
typedef std::basic_istream<char> cin;
typedef std::basic_istream<w_char> wcin;
Constructor:
explicit basic_istream( std::basic_streambuf<CharT, Traits>* sb);
protected:
basic_istream( const basic_istream& rhs ) = delete;
protected:
basic_istream( basic_istream&& rhs );
1,public explicit构造函数接受一个std::basic_streambuf作为参数.
2,protected拷贝构造函数且被delete了,因此是不支持拷贝的.
3,protected移动构造函数后续继承了改class的其他class可以使用移动构造函数.
std::basic_istream::operator=
protected:
basic_istream& operator=( const basic_istream& rhs ) = delete;
protected:
basic_istream& operator=( basic_istream&& rhs );
1,protected的拷贝赋值运算符且被delete了.
2,protected的移动赋值运算符后续继承了改class的其他class可以使用
std::basic_istream::get
int_type get();
basic_istream& get( char_type& ch );
basic_istream& get( char_type* s, std::streamsize count );
basic_istream& get( char_type* s, std::streamsize count, char_type delim );
basic_istream& get( basic_streambuf& strbuf );
basic_istream& get( basic_streambuf& strbuf, char_type delim );
需要特别的注意的是以下无论是哪种形式提取字符后该流内便不再有该字符了.
1,从输入流中提取一个字符,读取失败则给当前istream设置failbit和endbit.
2,提取count个字符存入到s指向的数组,当遇到换行符('\n')的时候也会停止读取.
3,提取count个字符存入到s指向的数组,当遇到换行符('\n')或者通过delim指定的字符的时候停止读取.
4,提取字符到std::basic_strembuf直到遇到换行符('\n')
5,提取字符到std::basic_streambuf直到遇到换行符('\n')或者通过delim指定的字符的时候停止读取.
std::basic_istream::peek
int_type peek();
从istream中读取一个字符,读取后该字符仍然在istream内.
std::basic_istream::unget
basic_istream& unget();
把最近一次提取出来的字符放回istream中.
Demo:
#include <sstream>
#include <iostream>
int main()
{
std::istringstream s1("Hello, world.");
char c1 = s1.get();
if (s1.unget())
{
char c2 = s1.get();
std::cout << "Got: " << c1 << " got again: " << c2 << '\n';
}
}
std::basic_istream::putback
basic_istream& putback( char_type ch );
把ch放入到istream,下一次从该istream提取字符的时候获得的就是ch.
std::basic_istream::getline
basic_istream& getline( char_type* s, std::streamsize count );
basic_istream& getline( char_type* s, std::streamsize count, char_type delim );
1,从istream中提取count个字符存入到s所指的数组中,直到遇到end of file为止.
2,从istream中提取count个字符存入到s所指的数组中,直到遇到end of file或者我们通过delim指定的字符为止.
std::basic_istream::ignore
basic_istream& ignore( std::streamsize count = 1, int_type delim = Traits::eof() );
从istream中提取并忽略前count个字符,或者遇到delim指定的字符的时候才停止忽略.
std::basic_istream::read
basic_istream& read( char_type* s, std::streamsize count )
通过istream提取count个字符存储到s中.当遇到end of file符号的时候会给当前流设置std::ios::failbit和std::ios::endbit.
std::basic_istream::readsome
std::streamsize readsome( char_type* s, std::streamsize count );
从istream提取count个字符存入s所指的数组,并返回实际存入数组中字符的数量.
std::basic_istream::gcount
std::streamsize gcount() const;
返回istream实际提取到的字符个数.
std::basic_istream::tellg
pos_type tellg();
返回读取位置,一般用于file stream和string streaam.
std::basic_istream::seekg
basic_istream& seekg( pos_type pos );
basic_istream& seekg( off_type off, std::ios_base::seekdir dir);
1,定位到绝对位置.
2,定位到相对位置,相对于(dir)的位置.
std::basic_istream::operator>>
basic_istream& operator>>( short& value );
basic_istream& operator>>( unsigned short& value );
basic_istream& operator>>( int& value );
basic_istream& operator>>( unsigned int& value );
basic_istream& operator>>( long& value );
basic_istream& operator>>( unsigned long& value );
basic_istream& operator>>( long long& value );
basic_istream& operator>>( unsigned long long& value );
basic_istream& operator>>( float& value );
basic_istream& operator>>( double& value );
basic_istream& operator>>( long double& value );
basic_istream& operator>>( bool& value );
basic_istream& operator>>( void*& value );
basic_istream& operator>>( std::ios_base& (*func)(std::ios_base&) );
basic_istream& operator>>( std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_istream& operator>>( basic_istream& (*func)(basic_istream&) );
basic_istream& operator>>( std::basic_streambuf<CharT,Traits>* sb );
提供格式化的输入操作.