C + + fragmented knowledge points

Source: Internet
Author: User

Stream iterators

2017-05-21 17:05:51

    • A stream iterator is a standard Template library in STL, a class template that can be used with any function that accepts a corresponding iterator after it is instantiated (you can think of a stream as a container, store the data in a contiguous buffer, have the function of an iterator, and a similar use).
    • Both Istream_iterator and ostream_iterator are in the definition of the <iterator> header file.
    • Any type that provides an input operator (>>) and an output operator (<<) can create Istream_iterator objects and Ostream_iterator objects, which overload both functions on their own classes:istream& Operator >> (IStream &is, &myclass c); and ostream& operator << (ostream &os, const &myclass c);

The ostream_iterator is an output stream iterator and also a class template. To define an output stream iterator, you must specify the type of the output object. The constructor for this class has two parameters: the Ostream object that the output iterator points to and a string value that represents the delimiter between the output objects. As a result, you can create an iterator object (that is, class template instantiation) as follows:

    • Ostream_iterator<int> (cout, "/n");--for direct use of output stream iterators
    • Ostream_iterator<int> Out_iterator (cout, "/n");--Use the pointer out_iterator to
The istream_iterator is an input stream iterator. To define an output stream iterator, you must specify the type of the output object. The constructor for this class has only one argument: The IStream object that the input iterator points to. Instantiate an input stream iterator:
    • istream_iterator<int> (CIN);--Direct use
    • istream_iterator<int> in_iterator (CIN);--Using the pointer name

 Note: When the input iterator parameter is empty (ref.: http://blog.csdn.net/fdl19881/article/details/6685744)
    • Use Copy (istream_iterator<int> (CIN),istream_iterator<int> (), Back_inserter (Ivec)), add input to the end of the container in turn , and ends with a file terminator or non-int type value.
    • As can be seen by the following program, the iterator automatically adds 1 after each dereference of the output iterator.
1#include <iostream>2#include <iterator>3 using namespacestd;4 5 voidMain ()6 {7ostream_iterator<int> out(cout,"  ");8* out=1;9* out=2;Ten      out; One}
View Code

  

C + + fragmented knowledge points

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.