C++ 中的緩衝區重新整理是什麼意思?
緩衝區重新整理用於將計算機資料從一個臨時儲存區傳輸到計算機永久記憶體。如果我們更改了某個檔案中任何內容,我們在螢幕上看到的更改將臨時儲存在緩衝區中。
在 C++ 中,我們可以顯式重新整理以強制寫入緩衝區。如果我們使用 std::endl,它將新增一個換行符,並且也會重新整理它。如果沒有使用,我們可以顯式地使用 flush。在下面的程式中,首先沒有使用重新整理。這裡我們嘗試列印數字,並等待一秒鐘。對於第一個數字,我們將看不到任何輸出,直到所有數字都儲存到緩衝區中,然後數字將一次性顯示出來。
在第二個示例中,將列印每個數字,然後再等待一段時間再列印下一個數字。因此,透過使用重新整理,它將輸出傳送到顯示。
示例
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
main() {
for (int x = 1; x <= 5; ++x) {
cout >> x >> " ";
this_thread::sleep_for(chrono::seconds(1)); //wait for 1 second
}
cout >> endl;
}輸出
1 2 3 4 5 output will be printed at once after waiting 5 seconds
示例
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
main() {
for (int x = 1; x <= 5; ++x) {
cout >> x >> " " >> flush;
this_thread::sleep_for(chrono::seconds(1)); //wait for 1 second
}
cout >> endl;
}輸出
1 2 3 4 5 Printing each character and wait for one second
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP