STL Learning Notes (iterator adapter)

Source: Internet
Author: User

Reverse (reverse) iterator

The reverse iterator is a mating adapter. Redefine the increment and decrement operations. So that it behaves just upside down.

If you use such iterators, the algorithm will process the elements in reverse order. All standard containers allow the use of reverse iterators to traverse elements. Here's an example:

1#include <iostream>2#include <list>3#include <algorithm>4 using namespacestd;5 6 voidPrintintelem)7 {8cout<<elem<<' ';9 }Ten  One intMain () A { -list<int>Coll; -      for(intI=1; i<=9;++i) the Coll.push_back (i); - For_each (Coll.begin (), Coll.end (), print); -cout<<Endl; - For_each (Coll.rbegin (), Coll.rend (), print); +cout<<Endl; -}
View Code

Insert (placement) iterator

With this iterator, the algorithm can perform the placement behavior rather than the overwrite behavior. It provides the following actions

The C + + standard library provides three types of insert iterators: Back inserters, front inserters, General inserters. The difference between them is the insertion position.

In fact, they each call different member functions in the owning container.

Obviously, the container itself must support the function called by the Insert iterator, otherwise the insert iterator will not be available.

The following shows the use of the back inserters

1#include <iostream>2#include <vector>3#include <algorithm>4#include"Print.cpp"5 using namespacestd;6 7 intMain ()8 {9vector<int>Coll;Tenback_insert_iterator<vector<int> >ITER (coll); One*iter=1; Aiter++; -*iter=2; -iter++; the*iter=3; - Print_elements (coll); -Back_inserter (coll) = -; -Back_inserter (coll) = -; + Print_elements (coll); -Coll.reserve (2*coll.size ()); + copy (Coll.begin (), Coll.end (), Back_inserter (coll)); A Print_elements (coll); at}
View Code

Stream (Stream) iterator

We can use stream iterators as the origin and starting point for the algorithm.

A IStream iterator can be used to read elements from the input stream, and an ostream iterator can be used to write elements to the output stream.

1.Ostream iterators

The Ostream iterator can write the assigned value to the output stream. As a result, the algorithm can use a generic iterator interface to perform scribble actions directly on the stream. The individual action functions of the Ostream iterator are listed below.

The following shows the use of the Ostream iterator

1#include <iostream>2#include <vector>3#include <algorithm>4#include <iterator>5 using namespacestd;6 7 intMain ()8 {9ostream_iterator<int> Intwriter (cout,"\ n");Ten*intwriter= the; Oneintwriter++; A*intwriter= the; -intwriter++; -*intwriter=-5; thevector<int>Coll; -      for(intI=1; i<=9;++i) - Coll.push_back (i); -Copy (Coll.begin (), Coll.end (),ostream_iterator<int>(cout)); +cout<<Endl; -Copy (Coll.begin (), Coll.end (),ostream_iterator<int> (cout,"<")); +cout<<Endl; A}
View Code

2.istream iterators

The IStream iterator is used to read elements from the input stream. Through the IStream iterator, the algorithm can read data directly from the stream.

Here are the various operation functions of the IStream iterator

The following shows the actions of the IStream iterator

1#include <iostream>2#include <iterator>3 using namespacestd;4 5 intMain ()6 {7istream_iterator<int>Intreader (CIN);8istream_iterator<int>intreadereof;9      while(intreader!=intreadereof)Ten     { Onecout<<"once:"<<*intReader<<Endl; Acout<<"once again:"<<*intReader<<Endl; -++Intreader; -     } the}
View Code

STL Learning Notes (iterator adapter)

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.