ios::sync_with_stdio(false); あなたは誰ですか.


C++でよく見かけるこいつ
#include<iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    return 0;
}
何でも知って使ってください.
ios::sync_with_stdio(false);使用理由は
これはcppのiostreamをcのstdioと同期させるためである.
その違いを見てみましょう.
まず
  • ,ios::sync with stdio(false);使用しない場合は
  • 🤷入力
    #include <iostream>
    
    using namespace std;
    
    int main(){
        ios::sync_with_stdio(true); // 디폴트임ㄴ
        cout << "야";
        printf("외");
        cout<<"수영장";
    }
    🤷‍♂️しゅつりょく
    야외수영장
  • ios::sync_with_stdio(false);
  • 🤷入力
    #include <iostream>
    
    using namespace std;
    
    int main(){
        ios::sync_with_stdio(false);
        cout << "야";
        printf("외");
        cout<<"수영장";
    }
    🤷‍♂️しゅつりょく
    외야수영장
    前者は順番に出力され,後者はそうではないことがわかる.
    すなわち、後者はstdioとiostreamの同期を実行しない.
    c++のみ独立したバッファを使用し、cと同時に使用できません.
    バッファの速度が低下し、実行速度が向上します.
    ただし、マルチスレッド環境では出力順序は保証されません.
    n/a.結論
    アルゴリズムはほぼ単一スレッド環境で実行される.
    ios::sync_with_stdio(false);使うといいですね!