[C + + learning notes] iterator

0x00 Preface

There may be grammatical errors and punctuation errors in the text, please understand;

If you find code errors or other problems in the article, please let us know. Thank you!

This document is a C + + note recorded by individuals while learning. It is not a tutorial. There will be some references to other people's articles in the notes. The quoted original text will not be specially marked, but a link to the original text will be given in the reference document

0x01 iterator

The iterator is a generalized pointer. STL algorithm uses the iterator to traverse the sequence of elements stored in the container. The iterator provides a method to access each element in the container. Although pointers are also iterators, iterators are more than just pointers. The pointer can point to an address in the memory, and the corresponding memory unit can be accessed through this address; The iterator is more abstract. It can point to a location in the container. We don't need to care about the real physical address corresponding to this location, but only need to access the elements of this location through the iterator.

In STL, a container is an encapsulated class template, its internal structure is unknown, and the container can only be used through the container interface.

The algorithm is to apply to a variety of containers, and the elements stored in each container can be of any type. How to use ordinary pointers as mediators? At this time, we must use a more abstract "pointer", which is the iterator.

Using iterators, algorithm functions can access elements at a specified location in a container without having to relate to the specific type of the element.

1. Input stream iterator and output stream iterator

1) Input stream iterator

The input stream iterator is used to continuously input some type of data from an input stream. It is a class template. For example:

template<class T>istream_iterator<T>;

Where T is the type of input data from the input stream using the iterator. Type T should meet two conditions: there is a default constructor; For this type of data, you can use "> >" to input from the input stream. An instance of an input stream iterator needs to be constructed by the following constructor:

istream_iterator(istream& in);

istream_ The iterator class template has a default constructor. The iterator constructed with this function points to the end of the input stream. Comparing an input stream with this iterator can determine whether the input stream ends.

2) Input stream iterator

The output stream iterator is used to continuously output some type of data to an output stream. It is also a class template. For example:

temp;ate<class T>ostream_iterator<T>

Where T represents the type of output data to the output stream. Type T requires a function: for this type of data, you can use "< <" to output to the output stream. An output stream iterator can be constructed with the following two constructors:

ostream_iterator(ostream& out);
ostream_iterator(ostream& out, const char *delimiter);

The out parameter of the constructor indicates that the data is output to the output stream. The parameter delimiter is optional and represents the separator between two output data.

Example: read several real numbers from standard input and output their squares respectively:

#include<iterator>
#include <iostream>
#include<algorithm>

using namespace std;

double square(double x){
	return x * x;
}
int main() {
	transform(istream_iterator<double>(cin), istream_iterator<double>(),
				ostream_iterator<double>(cout, "\t"), square);
	cout << endl;
	return 0;
}

Operation results:
Input:

0.5 1.1 0 -3 0.1

Output:

0.25	1.21	0	9	0.01	

Because the program will read data from the standard input stream until the end of the input stream, when running the program, after inputting data, you need to press Ctrl+Z and enter in windows and Ctrl+D in Linux to indicate the end of standard input.

2. Iterator classification

STL divides iterators into five categories according to their functions. These five categories correspond to five concepts. The relationship between these five concepts is as follows:

1) Input iterator

Input iterators can be used to read data from a sequence, but they may not be able to write data to it.

Input iterators support non repeatable one-way traversal of sequences.

The input iterator is only used as input to algorithms that only need to traverse the sequence once.

2) Output iterator

The output iterator allows data to be written to the sequence, but there is no guarantee that data can be read from it.

The output iterator also supports one-way traversal of the sequence.

3) Forward iterator

The concept of forward iterator is a sub concept of the two concepts of input iterator and output iterator. It supports both data reading and data writing.

Forward iterators support repeatable one-way traversal of sequences.

4) Bidirectional iterator

The concept of bidirectional iterator is a sub concept of unidirectional iterator. Based on the functions supported by one-way iterators, it supports the reverse movement of iterators.

5) Random access iterator

The concept of random access iterator is a sub concept of bidirectional iterator. On the basis of bidirectional iterators, it supports directly moving iterators forward or backward by n elements, so the function of random access iterators is almost the same as that of pointers.

The iterator obtained by the begin and end functions of the vector container vector is a random access iterator, and the pointer is also a random access iterator.

3. Interval of iterator

The formal parameters of STL algorithm often include a pair of input iterators, and the interval formed by them is used to represent the sequence of input data.

Example: an example of comprehensive use of several iterators

#include<algorithm>
#include <iterator>
#include<vector>
#include<iostream>
using namespace std;

template<class T, class InputIterator, class OutputIterator>
void mySort(InputIterator first, InputIterator last, OutputIterator result){
	vector<T>s;
	for(;first!=last;++first)
		s.push_back(*first);
	sort(s.begin(), s.end());
	copy(s.begin(), s.end(), result);
}
int main() {
	double a[5] = {1.2, 2.4, 0.8, 3.3, 3.2};
	mySort<double>(a, a+5, ostream_iterator<double>(cout, " "));
	cout << endl;
	mySort<int>(istream_iterator<int>(cin), istream_iterator<int>(), ostream_iterator<int>(cout, ""));
	cout << endl;
	return 0;
}

Operation results:
Input:

0.8 1.2 2.4 3.2 3.3 

Output:

-5-4-123568

The sample source codes appearing in all chapters of C + + language programming (4th Edition) will be uploaded to github one after another with the deepening of learning. The codes are manually entered and compiled by individuals. Some sample codes may not have comments: https://github.com/fyw4/C-plus-plus-learning-example
above.

Reference documents:
Zheng Li, Dong Yuan, he Jiangzhou C + + language programming (4th Edition) [M] Beijing: Tsinghua University Press.

Added by tcarnes on Thu, 13 Jan 2022 22:28:39 +0200