問題描述
包含的意義是什么
ios_base::sync_with_stdio(false);
cin.tie(NULL);
在 C++ 程序中?
在我的測試中,它加快了執行時間,但是否有我應該擔心的測試用例包含這個?
In my tests, it speeds up the execution time, but is there a test case I should be worried about by including this?
這兩個語句是否總是必須在一起,還是第一個就足夠了,即忽略cin.tie(NULL)
?
Do the 2 statements always have to be together, or is the first one sufficient, i.e., ignoring cin.tie(NULL)
?
此外,如果其值已設置為 false
,是否允許同時使用 C 和 C++ 命令?
Also, is it permissible to use simultaneous C and C++ commands if its value has been set to false
?
https://www.codechef.com/viewsolution/7316085
以上代碼運行良好,直到我在 C++ 程序中使用 scanf/printf
并將值設為 true
.在這種情況下,它給出了分段錯誤.對此有什么可能的解釋?
The above code worked fine, until I used scanf/printf
in a C++ program with the value as true
. In this case, it gave a segmentation fault. What could be the possible explanation for this?
推薦答案
這兩個調用意義不同,與性能無關;它加快了執行時間這一事實(或可能是)只是一個副作用.您應該了解它們各自的作用,不要因為它們看起來像是優化而盲目地將它們包含在每個程序中.
The two calls have different meanings that have nothing to do with performance; the fact that it speeds up the execution time is (or might be) just a side effect. You should understand what each of them does and not blindly include them in every program because they look like an optimization.
ios_base::sync_with_stdio(false);
這會禁用 C 和 C++ 標準流之間的同步.默認情況下,所有標準流都是同步的,這在實踐中允許您混合 C 和 C++ 風格的 I/O 并獲得合理和預期的結果.如果禁用同步,則允許 C++ 流擁有自己的獨立緩沖區,這使得混合 C 和 C++ 風格的 I/O 成為一種冒險.
This disables the synchronization between the C and C++ standard streams. By default, all standard streams are synchronized, which in practice allows you to mix C- and C++-style I/O and get sensible and expected results. If you disable the synchronization, then C++ streams are allowed to have their own independent buffers, which makes mixing C- and C++-style I/O an adventure.
還要記住,同步的 C++ 流是線程安全的(來自不同線程的輸出可能會交錯,但不會出現數據競爭).
Also keep in mind that synchronized C++ streams are thread-safe (output from different threads may interleave, but you get no data races).
cin.tie(NULL);
這將 cin
與 cout
分開.綁定流可確保在對另一個流進行每次 I/O 操作之前自動刷新一個流.
This unties cin
from cout
. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.
默認情況下,cin
綁定到 cout
以確保合理的用戶交互.例如:
By default cin
is tied to cout
to ensure a sensible user interaction. For example:
std::cout << "Enter name:";
std::cin >> name;
如果 cin
和 cout
是綁定的,你可以期待在程序提示用戶輸入之前輸出被刷新(即,在控制臺上可見).如果您解開流,程序可能會阻止等待用戶輸入他們的姓名,但輸入姓名"消息尚不可見(因為 cout
在默認情況下被緩沖,輸出被刷新/顯示在控制臺僅在需要時或緩沖區已滿時).
If cin
and cout
are tied, you can expect the output to be flushed (i.e., visible on the console) before the program prompts input from the user. If you untie the streams, the program might block waiting for the user to enter their name but the "Enter name" message is not yet visible (because cout
is buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).
因此,如果您將 cin
與 cout
解開,則必須確保每次要在等待輸入之前顯示內容時手動刷新 cout
在 cin
上.
So if you untie cin
from cout
, you must make sure to flush cout
manually every time you want to display something before expecting input on cin
.
總而言之,了解它們各自的作用,了解后果,然后決定您是否真的想要或需要速度提高的可能副作用.
In conclusion, know what each of them does, understand the consequences, and then decide if you really want or need the possible side effect of speed improvement.
這篇關于ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!