2019/8/4 data input / output

    Data input / output

  For a normal operation of the program, the input data quickly and correctly is an essential part. In OI, which is an essential skill. This time I summarize OI often occur data input / output operations.

  // variable name format example x alternative.

  1.cin / cost

  Header: <iostream> / <iostream.h>

  The official name for the input / output streams. It can be widely used direct input / output a plurality of types of data. Convenient and simple to use, but is time-consuming, more efficient than many stdio (3-4 times) slower.

  Format: cin >> x;

        cout << x;

  Sao Operation: iOS :: sync_with_stdio ( to false);

  Close iostream and stdio synchronization, speed up cin / cout speed (but it seems still more than scanf / printf slow a little bit).

  2.scanf/printf

  Header: <cstdio> / <stdio.h>

  The official name for the standard input / output. The format can be used to direct input / output various data. The use of more cumbersome, time-consuming short.

  Format: scanf ( "format identifier", & x)

        printf ( "format identifier", x)

  3. Read made fast / fast input

  Function <cstdio> / <stdio.h> in getchar () / putchar (): based on the headers. 

  Speed ​​(than scanf / printf), can only be used for inputting numbers, data for more applications.

  Format: x = re ();

     wr( x ) ;

  Code:

  Fast reading

 1  long long  re()
 2  {
 3     char c=getchar();long long  res=0;int fg=1;
 4     while('0'>c||c>'9') 
 5     {
 6         if(c=='-')fg=-1;
 7         c=getchar();
 8     }
 9     while('0'<=c&&c<='9') 
10     res=((res<<3)+(res<<1)+c-'0'),c=getchar();
11     return res*fg;
12  }

  Fast lose

void wr(int x) 
{
    if(x<0) 
    {
        putchar('-');
        x=-x;
    }
    if(x>=10)wr(x/10);
    putchar(x%10+'0');
}

  4.freopen

  Header: <cstdio> / <stdio.h>

  It is used to redirect input and output streams. You can change the input / output environment without changing the premise other code (output data to a file). OI is often used in the game and commissioning.

  Format: freopen ( "filename", "access" needs to redirect file stream)

  NOTE: "Access" generally "R & lt" (read only) or "W" (write only), typically redirect stdin stream file (denoted by the input stream) or stdout (labeled output stream).

  

  "In TC2.0, allowing not refer to this header file directly call functions, but this is not standard practice. Not recommended. In order to avoid problems in other IDE can not be compiled or executed appear." ------ Baidu Encyclopedia "stdio.h" entry.

  In some compilation environment without the header file <cstdio> and call one function does not alarm, but will hang when formal submission, remember to add manually.

 

  In addition, in the practical application also may be used fstream and other operations, but because of the importance of not high, so for the time being is not the table.

Guess you like

Origin www.cnblogs.com/randomaddress/p/11297880.html