Accelerating flow with Std::ios::tie and Std::ios_base::sync_with_stdio

Source: Internet
Author: User

Std::ios_base::sync_with_stdio

Static BOOL BOOL true );  

Synchronize with Cstdio stream [static] Toggle

Turns on or off synchronization of all standard iostream streams with their standard C-streams.

In fact, this means that C + + and C streams use the same buffers, so you can freely mix and match flows. Synchronizing C + + standard streams ensures thread safety.

By default, the Iostream object andcstdio流同步。如果同步关闭,C++标准流独立地缓冲输入输出,在某些情况下,这是相当快速的。


Parameters

Syn

A Boolean value that indicates whether to synchronize, true to turn on synchronization, and false to turn off synchronization.


return value

Returns the state of the synchronization before it is called. When it is called for the first time, it always returns TRUE.


Using the sample

#include <iostream>int  main () {    Std::cout.sync_with_stdio (false ) );     " a\n " ;    std::p rintf ("b\n");     " c\n " ;}


Program output

Bac




    对于str标准流,它与C f流同步时,下列三对函数的效果相同。

    (1)std::fputc(f, c) 与 str.rdbuf()->sputc(c)

Span style= "Font-family:verdana;" >    (2) std::fgetc (Fstr.rdbuf (->sbumpc (

Span style= "Font-family:verdana;" >    (3) std::ungetc (c, Fstr.rdbuf (->sputbackc (C


Std::ios::tie

Get (1) ostream* tie () const;

Set (2) ostream* tie (ostream* tiestr);

The first form returns a pointer to the current bound output stream.

The second form binds the object tiestr and returns a pointer to the stream that was bound before the call was invoked.

"Not to be Continued"


Algorithm race, close the iostream object and cstdio流同步以提高输入输出的效率。

    (调用ios::sync_with_studio(false))

    在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。

Accelerating flow with Std::ios::tie and Std::ios_base::sync_with_stdio

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.