Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the buffer of C++

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use C++ 's buffer zone". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use C++ 's buffer zone.

Buffer zone

The buffer is part of the memory space. A memory buffer is created for each data stream in memory.

The buffer is used to store and release the data in the stream, and the data in the buffer is the stream. In C++, input and output streams are defined as classes, and classes in C++ 's I _ stream class O library are called streams. Cout and cin are stream objects in the iostream stream class.

Why introduce a buffer?

Why should we introduce a buffer zone?

For example, when we take information from the disk, we first put the read data in the buffer, and then the computer takes the data directly from the buffer, and then reads it in the disk after the data in the buffer is taken, so that the times of reading and writing to the disk can be reduced. in addition, the operation of the buffer is much faster than that of the disk, so the application of buffer can greatly improve the running speed of the computer.

For example, we use the printer to print the document, because the printing speed of the printer is relatively slow, we first output the document to the corresponding buffer of the printer, and then the printer prints gradually, then our CPU can handle other things.

Now you basically understand that a buffer is a piece of memory that is used between the input and output device and the CPU to cache data. It enables low-speed input and output devices and high-speed CPU to work together, avoids low-speed input and output devices occupying CPU, liberates CPU, and enables it to work efficiently.

Type of buffer

There are three types of buffers: full buffering, row buffering, and no buffering.

1. Full buffering

In this case, the actual Iripple O operation does not occur until the standard Iripple O cache is filled. A typical example of full buffering is the reading and writing of disk files.

2. Row buffering

In this case, when a newline character is encountered in the input and output, the real Imax O operation is performed. At this point, the characters we enter are stored in the buffer first, and the actual Icano operation is not carried out until the enter key is pressed to wrap the line. The typical representative is keyboard input data.

3. No buffer

That is, without buffering, standard error stderr is typical, which enables error messages to be displayed directly as soon as possible.

Refresh of buffer

The following conditions cause a buffer flush:

1. When buffer is full

2. Execute the flush statement

3. Execute the endl statement

4. Close the file.

Example # include using namespace std;int main () {int n [5]; for (int I = 0; I

< 5; ++i) { cin >

> n [I]; cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report