About Ios::sync_with_stdio (false); and Cin.tie (0) accelerating C + + input and output streams

Source: Internet
Author: User

Original address: 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

When I look at someone's ACM code on the Internet, I find others input and output statements are always scanf and printf, a little puzzled, think they use C language, rather than C + +, but do a question today (Sort):

Found with other online experts using the exact same method, using scanf and printf code submitted after accepted, while using CIN and cout time Limit exceeded, the code is as follows:

Code One (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. }

Code two (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. }

It can be seen that the code idea is exactly the same, but the input and output methods are different, asked the teacher, plus this code after the use of CIN and cout can also be accepted:

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

Baidu a bit, the original and cin,cout the reason is low efficiency, because the first thing to output to buffer, and then output, resulting in reduced efficiency, and this paragraph can be to eliminate iostream input and output cache, can save a lot of time, so that efficiency and scanf and printf, It should also be noted that the header files used by scanf and printf should be stdio.h instead of iostream.

I do not know this pair of functions to live today, before encountering Cin Tle always foolishly changed to scanf, and even believe that C + + in the IO aspect of inefficient nonsense, but this is only C + + to be compatible with C and take the conservative measures.

Tie

The tie is a function that binds two stream, and the null argument returns the current output stream pointer.

  1. #include <iostream>
  2. #include <fstream>
  3. submain//////////////////////////////////
  4. int main(int argc, char *argv[])
  5. {
  6. STD::ostream *prevstr;
  7. STD::ofstream ofs;
  8. OFS. Open("test.txt");
  9. STD::cout << "Tie example:\n"; //Direct output to screen
  10. *std::cin.  Tie() << "This was inserted into cout\n"; the//NULL parameter call returns the default output stream, which is cout
  11. Prevstr = std::cin.  Tie(&ofs); //CIN binds OFS, returns the original output stream
  12. *std::cin.  Tie() << "This was inserted into the file\n"; //OFS, output to file
  13. STD::cin.  Tie(prevstr); //Recovery
  14. OFS. Close();
  15. System("pause");
  16. Return 0;
  17. }
  18. End sub//////////////////////////////////

Output:

    1. Tie Example:
    2. This was inserted into cout
    3. Please press any key to continue.  . .

Test.txt output at the same time in the current directory:

    1. This was inserted into the file
Sync_with_stdio

This function is a "compatibility stdio" switch, C + + in order to be compatible with C, to ensure that the program in use of std::p rintf and std::cout without confusion, the output stream is tied together.

Application

In ACM, there is often a large data set that causes CIN tle. At this time most people (including the original I also) think this is CIN's inefficient scanf error, and even rise to the C language and C + + language implementation efficiency level of the boring debate. In fact, as stated above, this is only a conservative approach to C + + for compatibility. We can unbind stdio before IO, so be careful not to mix cout and printf at the same time.

By default, CIN binds to cout, and flush is called each time the << operator is executed, which increases the IO burden. The tie (0) (0 for null) can be used to unbind the CIN from the cout, further speeding up the execution efficiency.

As shown below:

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

About Ios::sync_with_stdio (false); and Cin.tie (0) accelerating C + + input and output streams

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.