Using iostream Iterator

Source: Internet
Author: User

Previously only knew IteratorIterator class. Most of the usual programming is to take him through some kind of container.

Today, we have a new way of using it and record it.

For example, there is a task that reads a string of string elements from a standard input device, saves them in a vector, arranges them, and then writes them back to the standard output device, which is usually the case with large code. (already commented out)

#include <iostream>#include<iterator>#include<algorithm>#include<vector>#include<string>using namespacestd;intMain () {stringWord; /*vector<string>text;    while (Cin>>word) {text.push_back (word);    } sort (Text.begin (), Text.end ()); for (int ix = 0; IX < text.size (); ix++) cout << Text[ix] << ";*/Istream_iterator<string> is(CIN);//To bind is to a standard input deviceistream_iterator<string> EOF;//represents the next position of the last element to be read, and for a standard input device, the end-of-file represents the previous, as long as the IStream object is not assigned to the istream_iterator when it is defined, it represents end-of-file. vector<string>text; Copy ( is, EOF, Back_inserter (text));//Back_inserter is iterator Inserter, the so-called insertionadapter, avoids the use of the container's assignment, because using = must determine the size of a container beforehand. sort (Text.begin (), Text.end ()); Ostream_iterator<string> Os (cout," ");    Copy (Text.begin (), Text.end (), OS); return 0;}

However, the iostream iterator can be implemented using the above method (which is not commented out).

If you want to enter into a file, you can add the following code

Ifstream in_file (" file name "); Ofstream out_file (" file  name") ); if (!in_file | |! out_file) {    "Unable to open the necessary files.\n";     return -1}2017-04-21 21:55:35

Using iostream Iterator

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.