Introduction to C + + iterator iterators
Iterators can be used to access all the elements of a container class's envelope, which behaves like a pointer. For example, you can use an iterator to traverse the elements contained in a vector container. There are several iterators as follows:
Iterator Description
Input_iterator provides read-capable forward-moving iterators, which can be incremented (+ +), compared and dereferenced (*).
Output_iterator provides write-capable forward-moving iterators, which can be added (+ +), compared and dereferenced (*).
Forward_iterator an iterator that can move forward while having read and write functions. It also has the function of input and output iterators, and can store the value of the iterator.
Bidirectional_iterator bidirectional iterators, while providing read and write functions, with forward iterators, but can be used to increase (+ +) or reduce (-) operations.
Random_iterator Random iterators provide random read and write functions. Is the most powerful iterator, with the full functionality of a bidirectional iterator, while implementing pointer-like arithmetic and comparison operations.
Reverse_iterator is like a random iterator or bidirectional iterator, but its movement is reversed. (either a random iterator or a bidirectional iterator that moves in reverse direction.)
The vector container class has a random-access random iterator, which also means that it can use random read-write algorithms. Since random iterators have all the characteristics of other iterators, this means that algorithms designed for other iterators can also be used on vector containers.
Example of C + + iterator iterator operation
(This article is compiled by www.169it.com)
(1) Each container type defines its own iterator type, such as vector:
1 |
vector< int >::iterator iter; //这条语句定义了一个名为iter的变量,它的数据类型是由vector<int>定义的iterator类型。 |
(2) Use an iterator to read every element in the vector:
123456 |
vector< int > ivec(10,1); for (vector< int >::iterator iter=ivec.begin();iter!=ivec.end();++iter) { *iter=2; //使用 * 访问迭代器所指向的元素 } const_iterator: |
Only the elements in the container can be read, not modified.
123456789 |
for
(vector<
int
>::const_iterator citer=ivec.begin();citer!=ivec.end();citer++)
{
cout<<*citer;
//*citer=3; error
}
vector<
int
>::const_iterator 和
const
vector<
int
>::iterator的区别
const
vector<
int
>::iterator newiter=ivec.begin();
*newiter=11;
//可以修改指向容器的元素
//newiter++; //迭代器本身不能被修改
|
(3) Arithmetic operation of iterator:
Iterator you can assign the Iter+n,iter-n to a new Iteraor object in addition to the + +,--operation. You can also use one iterator minus the other iterator.
123 |
const vector< int >::iterator newiter=ivec.begin(); vector< int >::iterator newiter2=ivec.end(); cout<< "\n" <<newiter2-newiter; |
A typical STL code that uses vectors:
12345678910111213 |
#include <vector>
#include <iostream>
using
namespace
std;
int
main()
{
vector<
int
> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);
ivec.push_back(4);
for
(vector<
int
>::iterator iter = ivec.begin();1. iter != ivec.end(); ++iter)
cout << *iter << endl;
}
|
The following code generates and uses Iterators for vector container objects:
1234567891011 |
vector<
int
> the_vector;
vector<
int
>::iterator the_iterator;
for
(
int
i=0; i < 10; i++ )
the_vector.push_back(i);
int
total = 0;
the_iterator = the_vector.begin();
while
( the_iterator != the_vector.end() ) {
total += *the_iterator;
the_iterator++;
}
cout <<
"Total=" << total << endl;
|
Tip: By referencing an iterator (*), you can access the elements contained in the container.
Article reprinted from:[169it-Latest and most comprehensive it information ]
This article title :Introduction to C + + iterator iterators and examples of iterator iterator usage codes
Introduction to C + + iterator iterators and examples of iterator iterator usage codes