Go C + + 's tie () function

Source: Internet
Author: User

Look at C + + primer today and see the Ios::tie () function. Do not understand its meaning, and do not know the role. So I checked the internet.


The definition given by cplusplus.com is:

ostream* tie () const;//Returns a pointer to the bound output stream.
ostream* Tie (ostream* tiestr); The tiestr points to the output stream that is bound to the object and returns the last bound output stream pointer.


What do you mean?

This means that for an iOS (input and output stream) object, you Can "bind" an output stream to it. Executes without parameters, returns the output stream pointer of "bind", sets the bound object back with an output stream pointer as a parameter, and returns the predecessor bound object (pointer).

But what is "binding"?

For example, for the following program:

#include <iostream> #include <fstream>using namespace Std;int main () {  ofstream ofs;  Ofs.open ("test.txt");  Cin.tie (&OFS); Comment out this line try  *cin.tie () << "There'll be some text:";   Equivalent to OFS << "There'll be some text";  char c;  while (CIN >> c) {    ofs << C;  }  Ofs.close ();  return 0;}

  

Under the Linux Watch command (under Windows do not know anything similar, if not to use this method: Manually turn off and then open) real-time view test.txt files, you will find every time you hit a few words in the terminal press ENTER, Test.txt file in the text more than a few.
And if you comment out the line labeled in the code, you will find that Test.txt will only appear when the program is running at the end of the run (under Linux by pressing ctrl+d,windows with the CTRL + Z end input).
This is the effect of "binding", whenever a "bound" object is inconsistent or output operation, will automatically refresh the "bound" of the object's buffer, in order to achieve real-time effect

Go C + + 's tie () function

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.