Part10 generic Programming and C + + Standard Template Library 10.2 iterators

Source: Internet
Author: User

Iterators are the bridge between algorithms and containers
Iterators are used as elements in the access container
The algorithm does not directly manipulate the data in the container, but indirectly through the iterator
Algorithm and container Independent
Add new algorithms without impacting container implementations
Add new container, the original algorithm can also be applied

Input stream iterators and output stream iterators
Input stream iterator
Istream_iterator<t>
Constructs an input stream (such as CIN) as a parameter
Available * (p++) to get the next INPUT element
Output stream iterators
Ostream_iterator<t>
An output stream (such as cout) is required for construction
Available (*p++) = x outputs the x to the output stream

Both belong to the adapter
An adapter is an object that provides a new interface to an existing object
The input stream adapter and the output stream adapter provide an iterator interface for the stream object

//Example 10-2 reads a few real numbers from the standard input, respectively, their square output#include <iterator>#include<iostream>#include<algorithm>using namespacestd;DoubleSquareDoublex) {    returnx*x;}intMain () {transform (Istream_iterator<Double> (CIN), istream_iterator<Double> (), ostream_iterator<Double> (cout,"\ t"), square); cout<<Endl; return 0;}

Operations supported by iterators
Iterators are generalized pointers that provide pointers-like operations such as + +, *, and operators.
Input iterators
Can be used to read data from a sequence, such as an input stream iterator
Output iterators
Allows data to be written to the sequence, such as an output stream iterator
Forward iterators
Both an input iterator and an output iterator, and a one-way traversal of the sequence is possible
Bidirectional iterators
Similar to a forward iterator, but can traverse data in two directions
Random-Access iterators
is also a bidirectional iterator, but can jump between any two positions in the sequence, such as pointers, iterators using the Begin (), end () function of the vector

Classification of iterators

Interval of iterators
Two iterators represent an interval: [P1, p2]
The STL algorithm often takes the interval of an iterator as input, passing the input data
The legal interval
P1 after n (n > 0) Self-increment (+ +) operation satisfies P1 = = P2
The interval contains P1 but does not contain P2

Helper functions for Iterators
Advance (p, N)
N-Step self-increment operation on p
Distance (first, last)
Calculates the distance between first and last of two iterators, that is, the number of "+ +" operations performed on first can make

//Example 10-3 examples of using several iterators synthetically#include <iostream>#include<vector>#include<iterator>#include<algorithm>using namespacestd;//sorts the numeric values of N T types from the input iterator and outputs the result through the output iterator resulttemplate<classTclassInputiterator,classOutputiterator>voidMysort (inputiterator First, inputiterator last, outputiterator result) {vector<T>s;  for(; First! = last; first++) S.push_back (*First );        Sort (S.begin (), S.end ()); //to sort s, the parameter of the sort function must be a random-access iteratorCopy (S.begin (), s.end (), result);//outputs the s sequence through the output iterator}intMain () {//sorts the contents of the S array after the output    Doublea[5] = {1.2,2.4,0.8,3.3,3.2}; Mysort<Double> (a,a+5,ostream_iterator<Double> (cout," ")); cout<<Endl; //reads a number of integers from the standard input and outputs the sorted resultmysort<int> (istream_iterator<int> (CIN), istream_iterator<int> (), ostream_iterator<int> (cout," ")); cout<<Endl; return 0;}

Part10 generic Programming and C + + Standard Template Library 10.2 iterators

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.