Input and output is an essential part of every programming language, c++ is no exception, let’s explain the past life of c++ standard input and output.
1. Iostream and iostream.h
#include<iostream> // This is the standard header file after the 1998 standardization, using the declaration namespace STD
#include<iostream.h> // This is the standard header file, functions and classes are global
Copy the code
Iostream is now a standard in C++ that aims to allow C++ code to be ported and mixed embedded without the restriction of the.h extension and avoid the extra processing and modification caused by the.h extension.
Iostream contains the same basic functions as the corresponding iostream.h. Iostream defines the content in the namespace STD, while iostream.h defines the standard input and output functions in the global space for compatibility with C. In addition, iostream is recommended to use directly. After all, iostream.h is an old object many years ago, which is clearly not applicable in standard c++ and may be eliminated in the future.
Note: During the standardization process, the details of some parts of the library were changed, so the entities in the old header file and the new header file do not necessarily correspond exactly
Here’s a look at the differences in their use:
#include<iostream.h>Or is it#include<iostream>
using namespace std;
Copy the code
If you want to use standard c++ input and output, you need to add using namespace STD.
2. Sorting out the relationship between input and output streams
To understand the input/output streams of c++, we must look at the source of the input/output streams from the installation file.
- The basic_istream template class is declared in the istream header
- The ostream header declares the basic_ostream template class
- Iostream, iostream simply declares an istream object and three Ostream objects, as we’ll explain later
- Iosfwd, the iosFWD header file declares an instance of all the input and output classes of the template class
- Basic_ifstream: basic_ofSTREAM: basic_ofSTREAM: basic_ofSTREAM: basic_ofSTREAM: basic_ofSTREAM: basic_ofSTREAM: basic_ofSTREAM: basic_ofSTREAM
- Iomainip, iomainIP declares some operator with parameters
- Sstream: basic_stringbuf, basic_istringstream, basic_ostringstream, basic_ostringstream
- Streambuf basic_streambuf template class is declared in streambuf
Iosfwd instantiates the input/output class template. Here’s a snippet of code:
/// Base class for @c char streams.
typedef basic_ios<char> ios; / / the base class
/// Base class for @c char buffers.
typedef basic_streambuf<char> streambuf;
/// Base class for @c char input streams.
typedef basic_istream<char> istream;
/// Base class for @c char output streams.
typedef basic_ostream<char> ostream;
/// Base class for @c char mixed input and output streams.
typedef basic_iostream<char> iostream;
/// Class for @c char memory buffers.
typedef basic_stringbuf<char> stringbuf;
/// Class for @c char input memory streams.
typedef basic_istringstream<char> istringstream;
/// Class for @c char output memory streams.
typedef basic_ostringstream<char> ostringstream;
/// Class for @c char mixed input and output memory streams.
typedef basic_stringstream<char> stringstream;
/// Class for @c char file buffers.
typedef basic_filebuf<char> filebuf;
/// Class for @c char input file streams.
typedef basic_ifstream<char> ifstream;
/// Class for @c char output file streams.
typedef basic_ofstream<char> ofstream;
/// Class for @c char mixed input and output file streams.
typedef basic_fstream<char> fstream;
Copy the code
For the sake of presentation, we will use the above instance classes directly to refer to the template classes. The following diagram illustrates the relationship between these classes:
The arrows represent inherited relationships, and then the corresponding classes with the buf suffix are buffer classes used by other classes in the same column.
Take istream,ostream, and iostream as examples to look at the specific inheritance relationship as follows:
template<typename _CharT, typename _Traits>
class basic_istream : virtual public basic_ios<_CharT, _Traits>;
template<typename _CharT, typename _Traits>
class basic_ostream : virtual public basic_ios<_CharT, _Traits>;
template<typename _CharT, typename _Traits>
class basic_iostream
: public basic_istream<_CharT, _Traits>,
public basic_ostream<_CharT, _Traits>;
Copy the code
Basic_iostream is derived from basic_istream and basic_ostream. Basic_iostream is derived from basic_istream and basic_ostream. Note that virtual inheritance is used when inheriting from basic_ios to prevent ambiguity when multiple parent classes share the base class.
Note: Ambiguity means that a basic_iostream produces two basic_ios objects. With virtual inheritance, only one basic_ios object is generated, avoiding ambiguity.
I’d like to ask how many of you started out with iostream using cin and cout. In fact, from the iostream header, we can see that the cin object we use is an instance of istream and cout is an instance of ostream. Two other instances of ostream, cerr and clog, are declared in standard c++.