<iostream>头文件源码
时间: 2023-06-13 11:05:23 浏览: 91
下面是iostream头文件的源码:
```cpp
#ifndef _IOSTREAM_H
#define _IOSTREAM_H
#include <ios> // basic_ios, streampos, streamoff, etc.
#include <streambuf> // basic_streambuf
#include <istream> // basic_istream
#include <ostream> // basic_ostream
#include <cstdio> // EOF, FILE
#include <wchar.h> // wchar_t
namespace std {
extern istream cin; // standard input stream
extern ostream cout; // standard output stream
extern ostream cerr; // standard error (output) stream
extern ostream clog; // standard logging (output) stream
template <class charT, class traits = char_traits<charT>>
class basic_iostream : public basic_ios<charT, traits> {
public:
using char_type = charT;
using int_type = typename traits::int_type;
using pos_type = typename traits::pos_type;
using off_type = typename traits::off_type;
using traits_type = traits;
// constructors
explicit basic_iostream(basic_streambuf<charT, traits>* sb);
virtual ~basic_iostream();
// delete copy and move constructors and assignments
basic_iostream(const basic_iostream&) = delete;
basic_iostream& operator=(const basic_iostream&) = delete;
basic_iostream(basic_iostream&&) = delete;
basic_iostream& operator=(basic_iostream&&) = delete;
// basic_iostream operations
basic_ios<charT, traits>* tie() const;
basic_ios<charT, traits>* tie(basic_ios<charT, traits>* tiestr);
basic_streambuf<charT, traits>* rdbuf() const;
void rdbuf(basic_streambuf<charT, traits>* sb);
// conversions
operator void*() const;
bool operator!() const;
// unformatted input
int_type get();
basic_iostream<charT, traits>& get(char_type& c);
basic_iostream<charT, traits>& get(char_type* s, streamsize n);
basic_iostream<charT, traits>& get(char_type* s, streamsize n, char_type delim);
basic_iostream<charT, traits>& getline(char_type* s, streamsize n);
basic_iostream<charT, traits>& getline(char_type* s, streamsize n, char_type delim);
basic_iostream<charT, traits>& ignore(streamsize n = 1, int_type delim = traits::eof());
basic_iostream<charT, traits>& read(char_type* s, streamsize n);
streamsize readsome(char_type* s, streamsize n);
// unformatted output
basic_iostream<charT, traits>& put(char_type c);
basic_iostream<charT, traits>& write(const char_type* s, streamsize n);
// formatted input
basic_iostream<charT, traits>& operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_iostream<charT, traits>& operator>>(basic_istream<charT, traits>& (*pf)(basic_istream<charT, traits>&));
basic_iostream<charT, traits>& operator>>(basic_streambuf<charT, traits>* sb);
basic_iostream<charT, traits>& operator>>(bool& n);
basic_iostream<charT, traits>& operator>>(short& n);
basic_iostream<charT, traits>& operator>>(unsigned short& n);
basic_iostream<charT, traits>& operator>>(int& n);
basic_iostream<charT, traits>& operator>>(unsigned int& n);
basic_iostream<charT, traits>& operator>>(long& n);
basic_iostream<charT, traits>& operator>>(unsigned long& n);
basic_iostream<charT, traits>& operator>>(long long& n);
basic_iostream<charT, traits>& operator>>(unsigned long long& n);
basic_iostream<charT, traits>& operator>>(float& f);
basic_iostream<charT, traits>& operator>>(double& f);
basic_iostream<charT, traits>& operator>>(long double& f);
basic_iostream<charT, traits>& operator>>(void*& p);
basic_iostream<charT, traits>& operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_iostream<charT, traits>& operator>>(basic_istream<charT, traits>& (*pf)(basic_istream<charT, traits>&));
basic_iostream<charT, traits>& operator>>(basic_streambuf<charT, traits>* sb);
// formatted output
basic_iostream<charT, traits>& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_iostream<charT, traits>& operator<<(basic_ostream<charT, traits>& (*pf)(basic_ostream<charT, traits>&));
basic_iostream<charT, traits>& operator<<(basic_streambuf<charT, traits>* sb);
basic_iostream<charT, traits>& operator<<(bool n);
basic_iostream<charT, traits>& operator<<(short n);
basic_iostream<charT, traits>& operator<<(unsigned short n);
basic_iostream<charT, traits>& operator<<(int n);
basic_iostream<charT, traits>& operator<<(unsigned int n);
basic_iostream<charT, traits>& operator<<(long n);
basic_iostream<charT, traits>& operator<<(unsigned long n);
basic_iostream<charT, traits>& operator<<(long long n);
basic_iostream<charT, traits>& operator<<(unsigned long long n);
basic_iostream<charT, traits>& operator<<(float f);
basic_iostream<charT, traits>& operator<<(double f);
basic_iostream<charT, traits>& operator<<(long double f);
basic_iostream<charT, traits>& operator<<(const void* p);
basic_iostream<charT, traits>& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_iostream<charT, traits>& operator<<(basic_ostream<charT, traits>& (*pf)(basic_ostream<charT, traits>&));
basic_iostream<charT, traits>& operator<<(basic_streambuf<charT, traits>* sb);
};
// typedefs
using iostream = basic_iostream<char>;
using wiostream = basic_iostream<wchar_t>;
// manipulators
extern basic_istream<char>& ws(basic_istream<char>& is);
extern basic_istream<wchar_t>& ws(basic_istream<wchar_t>& is);
// object instances
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
// non-member functions
template <class charT, class traits>
basic_istream<charT, traits>& flush(basic_istream<charT, traits>& is);
template <class charT, class traits>
basic_ostream<charT, traits>& flush(basic_ostream<charT, traits>& os);
template <class charT, class traits>
basic_istream<charT, traits>& ws(basic_istream<charT, traits>& is);
template <class charT, class traits>
void swap(basic_ios<charT, traits>& a, basic_ios<charT, traits>& b) noexcept;
}
#endif /* _IOSTREAM_H */
```
该头文件定义了C++标准输入输出流(cin、cout、cerr、clog)以及基本的输入输出流对象。同时还定义了一系列输入输出的操作符和控制符。
阅读全文