The binding of the input and output stream pointers in Iostream/fstream, the use of the tie function.

Source: Internet
Author: User

To be compatible with the input and output of C, C + + uses the tie to flow input and output through the line bindings, so Cin/cout is not independent. The cout is also executed when CIN is executed. Vice versa.

By Defalut,cin are tied to Cout,and wcin are tied to wcout.

By default, CIN and cout are bound together, and wcin and Wcout are bound together.

In other words, by default, we perform

int a;cin>>a;

User input ABCD ' enter '

The process of doing this is to first input the ABCD into the stream buffer and then output from the buffer to a.

Same

cout<<"Enter anumber";

The procedure to do this is to first "Enter a number." Input into the buffer and then output from the buffer to the console.

Thus, CIN and cout are not actually two of the opposite opposing functions we imagined. Instead, the two functions, in fact, perform the same process, both with input and output. (provided that the default is the case)

It is because of this that when we encounter a large data set that causes CIN tle, we may think that this is why CIN is less efficient than scanf. The input buffer is actually the flush buffer, which takes up time.

Next, the related library function tie

Take a look at the definition of the tie function in the standard library, there's two overloads, both heavy loads.

<1const;
<2>basic_ostream<char_type,traits_type>* Tie (basic_ostream<char_type,traits_type>*

The first Overload: Returns a pointer to the tied output stream. Returns a pointer to the current bound stream directly.

Second Overload: Ties the object to and tiestr returns a pointer to the stream tied before the call, if any. Binds the current object to the Tiestr flow pointer and returns A stream pointer that was previously bound, or null if no other flow pointer was previously bound.

Two overloads the return value is a flow pointer, and the parameter of the overloaded <2> is a stream pointer to be bound.

Take a look at the following two examples

#01, unbind the default bindings, and speed up the input and output.

Like below.

using namespace Std;
void Main () {
int I;cin.tie (&cout); cout<<"Enter a number. " ;
Cin>>i;}

User Input 3 ' enter '

The procedure for code execution is to directly "Enter a number." Output to the console and read the user input 3 directly into I.

The middle does not pass through the buffer.

So when we want to read a lot of data, we can unbind the tie function to speed up the data read.

#02, specify a binding input and output stream for different input and output functions.

1 //redefine tied Object2#include <iostream>3#include <fstream>4 using namespacestd;5 intMain () {
6Ostream *Prevstr;7 ofstream ofs;8Ofs.open ("Test.txt");9cout <<"Tie example:\n";Ten*cin.tie () <<"This was inserted into cout"; OnePrevstr = Cin.tie (&ofs); A*cin.tie () <<"This was inserted into the file"; - Cin.tie (PREVSTR); - ofs.close (); the return 0; -}

Binds the standard input and the file output.

Code Execution Results:

Tie Example:
This was inserted into cout

Generate test file at the same time

This was inserted into the file

This is because the first *cin.tie () is equivalent to the cout default binding is cout.

A second *cin.tie () is equivalent to OFS.

The binding of the input and output stream pointers in Iostream/fstream, the use of the 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.