關於ios::sync_with_stdio(false);和 cin.tie(0)加速c++輸入輸出流


原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html

               http://www.clanfei.com/2012/03/235.html

在網上查看別人的ACM代碼時,發現別人輸入輸出語句用的總是scanf與printf,有點不解,還以為他們用的都是C語言,而非C++,但今天做的一道題(Sort):

發現與網上的其他高手使用完全相同的方法,使用scanf及printf的代碼提交后Accepted,而使用cin及cout的卻Time Limit Exceeded,代碼如下:

代碼一(Accepted):

  1. #include<iostream>
  2. using namespace std;
  3. bool a[1000001];
  4. int main()
  5. {
  6. int n, m, num, count;
  7. while(scanf("%d%d",&n,&m)!=EOF){
  8. memset(a, 0, sizeof(a));
  9. for(int i=0; i<n; i++){
  10. scanf("%d",&num);
  11. a[num + 500000] = 1;
  12. }
  13. count = 0;
  14. for(int j = 1000000; j >= 0; --j){
  15. if(a[j]){
  16. if(count == m - 1){
  17. printf("%d\n",j-500000);
  18. break;
  19. }
  20. printf("%d ",j-500000);
  21. count++;
  22. }
  23. }
  24. }
  25. return 0;
  26. }

代碼二(Time Limit Exceeded):

  1. #include<iostream>
  2. using namespace std;
  3. bool a[1000001];
  4. int main()
  5. {
  6. int n, m, num, count;
  7. while(cin >> n >> m){
  8. memset(a, 0, sizeof(a));
  9. for(int i=0; i<n; i++){
  10. cin >> num;
  11. a[num + 500000] = 1;
  12. }
  13. count = 0;
  14. for(int j = 1000000; j >= 0; --j){
  15. if(a[j]){
  16. if(count == m - 1){
  17. cout << j - 500000 << endl;
  18. break;
  19. }
  20. cout << j - 500000 << " ";
  21. count++;
  22. }
  23. }
  24. }
  25. return 0;
  26. }

可以看出,代碼思路完全一樣,只是輸入輸出方法不同,問過老師,加上這一句代碼后使用cin及cout也可以Accepted:

  1. std::ios::sync_with_stdio(false);

百 度了一下,原來而cin,cout之所以效率低,是因為先把要輸出的東西存入緩沖區,再輸出,導致效率降低,而這段語句可以來打消iostream的輸入 輸出緩存,可以節省許多時間,使效率與scanf與printf相差無幾,還有應注意的是scanf與printf使用的頭文件應是stdio.h而不是 iostream。

 

 

我是怎么在不知道這一對函數的情況下活到今天的,以前碰到cin TLE的時候總是傻乎乎地改成scanf,甚至還相信過C++在IO方面效率低下的鬼話,殊不知這只是C++為了兼容C而采取的保守措施。

tie

tie是將兩個stream綁定的函數,空參數的話返回當前的輸出流指針。

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. ///////////////////////////SubMain//////////////////////////////////
  5. int main(int argc, char *argv[])
  6. {
  7. std::ostream *prevstr;
  8. std::ofstream ofs;
  9. ofs.open("test.txt");
  10.  
  11. std::cout << "tie example:\n"; // 直接輸出到屏幕
  12.  
  13. *std::cin.tie() << "This is inserted into cout\n"; // 空參數調用返回默認的output stream,也就是cout
  14. prevstr = std::cin.tie(&ofs); // cin綁定ofs,返回原來的output stream
  15. *std::cin.tie() << "This is inserted into the file\n"; // ofs,輸出到文件
  16. std::cin.tie(prevstr); // 恢復
  17.  
  18. ofs.close();
  19. system("pause");
  20. return 0;
  21. }
  22. ///////////////////////////End Sub//////////////////////////////////

輸出:

  1. tie example:
  2. This is inserted into cout
  3. 請按任意鍵繼續. . .

同時當前目錄下的test.txt輸出:

  1. This is inserted into the file

sync_with_stdio

這個函數是一個“是否兼容stdio”的開關,C++為了兼容C,保證程序在使用了std::printf和std::cout的時候不發生混亂,將輸出流綁到了一起。

應用

在ACM里,經常出現 數據集超大造成 cin TLE的情況。這時候大部分人(包括原來我也是)認為這是cin的效率不及scanf的錯,甚至還上升到C語言和C++語言的執行效率層面的無聊爭論。其 實像上文所說,這只是C++為了兼容而采取的保守措施。我們可以在IO之前將stdio解除綁定,這樣做了之后要注意不要同時混用cout和printf 之類。

在默認的情況下cin綁定的是cout,每次執行 << 操作符的時候都要調用flush,這樣會增加IO負擔。可以通過tie(0)(0表示NULL)來解除cin與cout的綁定,進一步加快執行效率。

如下所示:

    1. #include <iostream>
    2. int main() 
    3. {
    4.     std::ios::sync_with_stdio(false);
    5.     std::cin.tie(0);
    6.     // IO
    7. }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM