Read the new posture of optimization-Learning notes __ Other

Source: Internet
Author: User

shock. 99.9% of Oier don't know. Using this method, the violence immediately changed the scale.

Recently learned a new function fread, you can read the entire block from the file into the information, fast. Based on this function, we can write a new GetChar ():

Inline Char GC () {
    static char buf[100000],*p1=buf,*p2=buf;
    Return p1==p2&& (p2= (p1=buf) +fread (Buf,1,100000,stdin), P1==P2)? eof:*p1++;
}

Fread's 4 parameters represent where to start saving, a number of bytes (char is 1), and want to read a few, input files. After execution, it returns a few successful reads.
The BUF array is a note of the read information. The p1p2 points to the head and tail in the current buf, respectively. If P1==P2 explains that you need to fread a whole block again.
Use it to write read excellent effect is very good, the input stream is very large when compared with GetChar () read more than twice times faster.
The complexity of the scale is relatively tight, write a metaphysical violence plus reading may be AC.
But be sure to note that this cannot be mixed with scanf. SCANF can be used before the first tuning of GC ().

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.