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 understand C++ string input buffer mechanism

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

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the C++ string input buffer mechanism". The content of the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn how to understand the C++ string input buffer mechanism.

1. Definition of buffer 1. Buffer definition

Buffering is a common means to smooth the transmission process when transferring information between two different speed devices.

two。 Why a buffer is introduced

The operating system course clearly explains the role of buffering in order to solve the speed mismatch between high-speed and low-speed devices. Give a direct example of CPU and printers in the book:

First of all, if we want to print an article on the printer, we must go through CPU processing and then print the article to the printer, but CPU handles 1000 bytes of text in the blink of an eye, but the printer may take several seconds, so it is impossible for CPU to process a data and give the printer a data waiting to run. This introduces a buffer, and the CPU processes the data into the buffer. The printer extracts the processed data directly from the buffer, which solves the problem of processing speed mismatch between high-speed devices (CPU) and low-speed devices (printers).

II. Buffer types of scanf,cin input buffers 1.scanf and cin

The buffering type of scanf and cin is line buffering, which is characterized by storing the input data in the input buffer as long as it does not encounter a newline character (carriage return). When it encounters a newline character, it will take out the data in the buffer and use it.

Buffering Mechanism of 2.scanf and cin

The input data buffering mechanism of scanf and cin is basically the same. When reading a data, it will be stored in the input buffer until the enter is entered, and the data will not be taken out of the input buffer for use by variables until the carriage return is encountered, but the newline characters in the buffer will be left in the input buffer.

3.cin.getline and cin.get

Cin.get reads the string until it is read to the carriage return, but also leaves the carriage return in the buffer.

Cin.getline reads the string until it is read to the carriage return, but does not say that the carriage return is left in the buffer.

4.scanf and cin input

The difference between ① cin and scanf reading a character

Char c; cin > > scanf Paccin will not read spaces, carriage returns and tabs when reading characters. If the buffer begins with a newline character or tabs, it will be ignored and scanf ("% c", & c) will be cleared; / / the characters will be read directly regardless of what characters you are reading.

② cin and scanf do not read opening spaces, newline characters, or tabs as data when reading numbers, strings, and floating-point types in buffers, but ignore them and clear them if they are encountered. It is important to note that cin.get and cin.getline do not ignore and delete opening spaces, newline characters, or tabs.

5. Problems that may be encountered

After ① reads a string with cin or scanf, scanf reads another character and finds that the read character cannot be entered and outputs a carriage return. Look at the following code:

Char name [20]; char c; cin > > name; 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