Introduction to C++ header files — <iostream> header files

Introduction to C++ header files— <iostream>header files

<iostream>head File

Introduction

To perform any input and output operations in C++, we need to use the iostream header. Without <iostream>the header file, there is no way to get input from the user or print any output.

import <iostream>Behaves as if defining a std::ios_base::Initstatic storage-duration object of type : if it is the first object constructed std::ios_base::Init, its constructor initializes each standard stream object, and if it is the last object destroyed std::ios_base::Init, its destructor These objects (except cin and wcin) will be flushed.

<iostream>The library is considered to be the most basic and important header file in C++, which defines the input and output stream classes and functions, and can realize the interactive operation between the program and the console or files. Knowledge of libraries <iostream>is essential to using C++ for input and output operations.

overview

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
 
namespace std {
    
    
 
    extern istream cin;
    extern ostream cout;
    extern ostream cerr;
    extern ostream clog;
 
    extern  wistream wcin;
    extern  wostream wcout;
    extern  wostream wcerr;
    extern  wostream wclog;
 
}

Development History

Many people who learn C++ know that there is one in C++ <iostream.h>. This was used by earlier versions of C++ and is not supported by current versions. The current version uses iostream. After the early version of C++ came out, the industry generally responded <iostream.h>slowly, and C++ was constantly improving. In a certain version, a new input and output stream was written. This input and output stream has a faster speed and is generally accepted by the industry. . In order to highlight the difference between C++ and C, as well as the progress of C++ in the past, and the compatibility with the early language of C++, the C++ standard named this input and output stream iostream, and at the same time retained it. These two files can be edited in Viual <iostream.h>Studio See External Dependencies in the . Their codes are not the same.

composition

<iostream>At the base of the library are two types named istream and ostream, which represent input and output streams, respectively. A stream is a sequence of characters to be read from or written to an IO device. The term "stream" attempts to describe that characters are produced or consumed sequentially over time.

The standard library defines 4 44 IO objects. An object of type istream namedcin(pronounced see-in) is used when processing input. This object is also called standard input. Output is processed usingcoutan object of type ostream named (pronounced see-out), also known as standard output. The standard library also defines two other ostream objects, namedcerrand clog (pronounced "see-err" and "see-log", respectively). cerrThe object is also called standard error, and it is usually used to output warning and error messages to the user of the program. Objectsclogare used to generate general information about program execution.

base class template

<iostream(.h)>The basis of the library is a hierarchy of class templates. Class templates provide most of the library's functionality in a type-independent manner.

The basic class template is a collection of class templates, where each class template has two parameters: the character type (charT) parameter determines the element type to be processed, and the attribute parameter provides some additional characteristics for each specific element type.

Class templates in this class hierarchy have the additional prefix basic_ in front of their names compared to instances of their character types.

For example, the class template from which istream is instantiated is named basic_istream, the class template from which fstream is instantiated is named basic_fstream, and so on. The only exception is ios_base, which is not based on any class template because it is type-independent, but a regular class.

class template instance

<iostream(.h)>Two sets of standard instances of the entire iostream class template hierarchy are integrated in the library: one set is single-byte-oriented and handles elements of type char; the other set is wide-byte-oriented and handles elements of type wchar_t.

Single-byte (char) oriented instances are probably <iostream(.h)>a better known part of the library. Classes such as ios, istream, and ofstream are all single-byte oriented. Figure 1 shows the names and relationships of all classes for a single byte. Wide-byte-oriented (wchar_t type) instances are named the same as single-byte-oriented instances, but all class and object names are prefixed with www , such as wios, wistream, and wofstream.

standard object

As <iostream(.h)>part of the library, the header file declares some objects used to perform input and output operations on the standard input and output devices.

These objects are divided into two groups: single-byte-oriented, namely the common cin, cout, cerr and clog;

Its wide-byte-oriented counterparts are declared as wcin, wcout, wcerr, and wclog.

type

<iostream(.h)>Classes in libraries rarely use primitive types for the prototypes of their members, but typically use types defined in terms of the properties of their instances. For instances of the default char and wchar_t types, the types streampos, streamoff, and streamsize are used to represent position, offset, and size, respectively.

manipulator

<<Manipulators are global functions for use with the insertion ( ) and extraction ( >>) operators that operate on stream objects . They usually change the attributes and formatting of the stream. endl, hex, and scientific are some examples of manipulators.

<iostream>Syntax of header files

#include <iostream.h>  
#include "iostream.h"\n

<iostream>There are two types of streams in header files:

  1. Input Stream: To get any input from the user, you need to use cin, which belongs to the input stream.

Syntax for using input streams:std::cin>> variable_name

When cin is executed, the cursor will stop at the specified statement until a value is entered. The entered value will be stored in a variable.

  1. Output stream: To print output, use the built-in function cout on the output stream:

Syntax of cout:std::cout<<variable_name

When cout is executed, the value in the variable is printed.

content

// iostream standard header
#if     _MSC_VER > 1000
#pragma once
#endif
#ifndef _IOSTREAM_
#define _IOSTREAM_
#include <istream>
#ifdef  _MSC_VER
#pragma pack(push,8)
#endif  /* _MSC_VER */
_STD_BEGIN 
         // OBJECTS
static ios_base::
Init _Ios_init;
extern _CRTIMP istream cin;
extern _CRTIMP ostream cout;
extern _CRTIMP ostream cerr, clog;
          // CLASS _Winit
class _CRTIMP _Winit {
    
    
public:
     _Winit(); 
     ~_Winit();
private: 
    static int _Init_cnt;
     }; 
          // WIDE OBJECTS
static _Winit _Wios_init;
extern _CRTIMP wistream wcin;
extern _CRTIMP wostream wcout, wcerr, wclog;
_STD_END
#ifdef  _MSC_VER
#pragma pack(pop)
#endif  /* _MSC_VER */
 
#endif /* _IOSTREAM_ */
/*
 * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
 * Consult your license regarding permissions and restrictions.
 */

Guess you like

Origin blog.csdn.net/ZH_qaq/article/details/131342626