日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++中关于cin.tie以及sync_witch_stdio同步

發(fā)布時(shí)間:2024/3/12 c/c++ 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++中关于cin.tie以及sync_witch_stdio同步 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

flush()

ostream類的一個(gè)方法,作用是將緩沖區(qū)的數(shù)據(jù)刷新到輸出文件。

/* Flushing files (flush manipulator) */#include <ostream> /* std::flush*/#include <fstream> // std::ofstream#include <bits/stdc++.h>int main () {std::ofstream outfile ("test.txt");for (int n=0; n<100; n++){outfile << n;_sleep(1000); // 1soutfile.flush(); //<--}outfile.close();return 0;}

執(zhí)行outfile.flush()時(shí),把outfile緩沖區(qū)中的數(shù)據(jù)輸出到test.txt中。因此test.txt中每隔一秒新增一個(gè)數(shù)字。
若是刪除outfile.flush()這句,則100s以內(nèi)test.txt沒有增加內(nèi)容。
執(zhí)行outfile.close()是outfile緩沖區(qū)的數(shù)據(jù)被一次性flush輸出到test.txt中。
因此flush()的作用是將刷新當(dāng)前緩沖區(qū)的數(shù)據(jù),將其輸出到輸出文件中。

cin.tie()

cin is tied to the standard output stream cout (see ios::tie), which indicates that cout’s buffer is flushed (see ostream::flush) before each i/o operation performed on cin.

沒有綁定的輸入輸出流的i/o操作互相獨(dú)立,互不影響。cin和cout綁定后,cin之前會(huì)將cout輸出緩沖區(qū)的數(shù)據(jù)刷新到輸出文件中。
這樣可以避免cout語句在cin之前,而命令行窗口中cin的讀入數(shù)據(jù)在cout輸出數(shù)據(jù)之前。

ios::sync_with_stdio(default=true)

If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the thread.

iostream 和cstdio綁定。這樣為了避免混用printf和cout而造成的輸出順序和代碼語句不一致的問題。
解綁的話,輸出結(jié)果無法預(yù)測(cè)。

總結(jié)

以上是生活随笔為你收集整理的c++中关于cin.tie以及sync_witch_stdio同步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。