C ++ most people regard cin: sync () as misuse of the cache function, cinsync

Source: Internet
Author: User

C ++ most people regard cin: sync () as misuse of the cache function, cinsync

 

 

One hundred degrees, most people think of the cin: sync () function as a function to clear the buffer. However, if we use the VS2017 compiler, we will find that the buffer cannot be cleared. Why?

 

Http://en.cppreference.com/w/cpp/io/basic_istream/sync

 

Follow the instructions in this standard document:

This function is used:Synchronizes the input buffer with the associated data source.

 

Then focus on:

As with readsome (), it is implementation-defined whether this function does anything with library-supplied streams. the intent is typically for the next read operation to pick up any changes that may have been made to the associated input sequence after the stream buffer last filled its get area. to achieve that,sync()May empty the get area, or it may refill it, or it may do nothing. A notable exception is Visual Studio, where this operation discards the unprocessed input when called with a standard input stream.

 

This function is:Implementation-defined

 


Therefore, the implementation may not necessarily clear the buffer. The specific implementation is related to the compiler, so it does not have good portability. Use it with caution!

 

For details, refer to the specific instructions on this website!

Related Article

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.