c ++ base - digital reading and optimization

(First blog, poorly written, please forgive me).

A, cin

As we all know, cin can read a lot of things:

int a;
long long b;
double c; cin>>a>>b>>c;

It is wonderful to understand, do not even have to write something else, you can read any number.

But cin have a great disadvantage: time-consuming.

In my many years of experience in water issues, cin data about 100000 times out, light read on variable it has been lost, the face of post-treatment variables not to mention;

Resulting in slow cin cin fundamental reason is because the default stdin and to keep pace, it will consume a lot of useless time.

You might have thought the big brother: Since synchronized time out, then shut the synchronization is not good enough?

So with the following piece of code:

std::ios::sync_with_stdio(false);

This code is usually written at the beginning of the main function, the significance is close to stdio compatible, it may have an impact on scanf and printf, however. . . Speed ​​is still not as scanf. (Laughing cry)

So in addition to a string of related topics, I always use scanf or fast read.

Two, scanf

c is an input function scanf language, may also be used in c ++ language.

Opera input stream (refer CIN) c ++ compared to the much faster, but it is to control format. . .

The following table shows some common formats

It is noted that    ld equal to d, lld is long integer (corresponding to the long long), is not the same for f, lf corresponds to double, f corresponding to the float, without llf.

There are a few special attention to:

First, do not enter spaces in the format control characters when read into digital, so they may be wrong. (Personal experience)

Second, before the digital format control characters can be added to control the width, i.e. read few, expressed as% 5d reads five digits.

(C) before adding * as format (e.g.,% * d) can be ignored to specify variables,% * 1d% d can be removed first digit read.

Third, fast read

Usually scanf on basic enough, but encountered some problems cancer emmm, or quickly read it.

Fast read basic principles: character read a lot faster than the digital read (probably eliminating the judge is not digital operation ??)

First put some quick reading of code:

int read(){
    int x=0,t=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-') t=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return x*t;
}

For the interpretation of the code:

       x represents the current value, t indicates whether the number is negative, c is the current read characters.

       while the second line of the statement can be effectively read out line breaks and spaces.

       After reading the numbers, each read a number, multiplied by the number before it will be coupled with the current number of 10, stop reading spaces.

       10 how by: bit left shift operation is, the left represents a binary count of a full left, i.e. multiplied by 2, the left three left represents three, i.e. multiplied by 2 ^ 3 = 8, add up to 10-bit computing than using multiplication save time.

       On how the current number plus: ascll code '0' is the first 48, 48 is represented binary 110000, or 48 different characters can be up to two binary numbers (i.e., 48) is removed, leaving the current digital .

       Reserved bit binary number two digits are not the same: // on XOR.

Precautions: 1. arithmetic left and right shifts of magnitude lower than addition and subtraction, exclusive OR operation lower than the right and left, to ensure that priority parentheses.

                   2. Fast Read only integer used to read the specified type.

 

I remember like it a point of concern Oh!

(Of course, I do not like comments also possible)

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/kafuuchinoQWQ/p/11222306.html