Introduction to C + + iterator iterators and examples of iterator iterator usage codes

Source: Internet
Author: User

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 和 constvector<int>::iterator的区别constvector<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 constvector<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>  usingnamespacestd;  intmain() { 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;  forinti=0; i < 10; i++ )    the_vector.push_back(i);  inttotal = 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

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.