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

來源:互聯網
上載者:User

標籤:.com   恢複   tag   c++語言   bool   加速   class   turn   指標   

原文地址: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. }

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.