C ++ input/output streams are used to replace C's standard Io functions.

Source: Internet
Author: User

This articleArticleThis is not to introduce some basic knowledge and usage of the input and output stream of C ++. Here, we want to persuade everyone to use the input and output stream of C ++.

Cben has provided enough API operationsCompositionWhy does C ++ provide input and output streams? The following are some reasons why I recommend C ++ stream:

1. The concept of stream is closerProgramThe concept of input and output. The information is like water. Inbound from here, outbound from there, stream, and stream ~~~

2. Stream provides an object-oriented method for processing input and output, in line with the original intention of the C ++ design.

3. Well, I admit that there are some elements of the above two points. Let's talk about some practical points.

4. C's scanf/printf functions are flexible, but there are security risks:

Take a look at the following:Code:

 
Fprintf (stdout, "% d \ t % F \ t % s", 10, 10, "SS ");

 

What will be output?

Scanf/printf functions cannot check the consistency between the actual parameters and required parameters. This problem cannot be found in the compilation phase and will only occur during the runtime, and it is not always a problem, it is often because the data in the program is damaged, which leads to some strange problems. You may be confused by the second monk, and then work overtime to find bugs.

The C ++ Stream does not have this low-level problem ^_^

5. Take C's scanf/printf class and read the following code:

STD: String Ss = "sssss"; fprintf (stdout, "% s", SS );

 

The input and output functions of C are limited. They only support a limited number of basic data types. Some extension types and user-defined types are also excluded, do you want me to support you? No!

The C ++ stream supports not only basic types, but also extension types. There is only one simple requirement, that is, our type requires the "<" or ">" operator function of this type. The following example runs perfectly:

 
Class myadd {public: myadd (int I, Int J): _ I (I), _ j (j) {} PRIVATE: int _ I; int _ j; friend STD:: ostream & operator <(STD: ostream & OS, const myadd & self) ;}; STD: ostream & operator <(STD: ostream & OS, const myadd & self) {return OS <self. _ I + self. _ j;} // The following is the test code STD: cout <myadd (3,5) <STD: Endl;

 

 

6. C ++ provides great support for streaming. As an object, a stream can be perfectly integrated with STL. Let's look at some examples:

# Include <iostream> # include <sstream> # include <ostream> # include <iterator> int _ tmain (INT argc, _ tchar * argv []) {STD :: copy (STD: istream_iterator <int> (STD: CIN), STD: istream_iterator <int> (), STD: ostream_iterator <int> (STD :: cout); Return 0 ;}

 

The above code outputs the standard input directly to the standard output until the input is not an integer.

The above functions are of course very simple, and there is not much code to implement with C, but the key here is to let us see the ability to combine stream and STL containers, how seamless this is.

Another example is to output the data in the vector to a file. Each data is separated by '\ n.

 
# Include <iostream> # include <sstream> # include <fstream> # include <iterator> # include <vector> int _ tmain (INT argc, _ tchar * argv []) {STD: vector <STD: String> V (10, "Hello World"); STD: Copy (v. begin (), V. end (), STD: ostream_iterator <STD: String> (STD: ofstream ("C: \ 11.txt")," \ n "); Return 0 ;}

 

There are many similar applications. Actually, STLAlgorithmAny algorithm that supports the input or output iterator can use the iterator to operate the corresponding Stream object. This undoubtedly provides strong background support for streaming.

After talking so much about it, it is still a bitter sentence: mix it with C ++, and discard the standard Io of C!

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.