Introduction to the C ++ iterator

Source: Internet
Author: User

An iterator is a data type that checks elements in a container and traverses elements.

The standard library defines an iterator type for each standard container. The iterator type provides a more generic method than the subscript operation: All standard library containers define the corresponding iterator type. Because the iterator applies to all containers, modern c ++ programs prefer to use the iterator instead of subscript operations to access container elements.

 

1. Container iterator type

Each container type defines its own iterator type;

Vector <int>: iterator ITER;

Each standard library container type defines a member named iterator. The iterator here has the same meaning as the actual type of the iterator.

 

2 begin and end operations

Each container defines a function named begin and end for returning the iterator. If the container contains elements, the iterator returned by begin points to the first element.

Vector <int> ivec;

Vector <int>: iterator iter = ivec. Begin (); equivalent to ivec [0];

The iterator returned by the end operation points to the "next to the end element" of the vector ". This is a nonexistent element because it has exceeded the subscript. If the vector is empty, the iterator returned by begin and end is the same.

 

The iterator returned by the end operation does not point to any actual elements in the vector. On the contrary, it only acts as a sentry, indicating that all elements of the vector have been processed.

 

3. Auto-increment and reference operations of the vector iterator

The iterator type defines some operations to get the elements pointed to by the iterator, and allows the programmer to move the iterator from one element to another.

 

The iterator type can be accessed by using the unreferenced operator.

Vector <int>: iterator iter = ivec. Begin ();

Int A = * ITER; equivalent

Int A = ivec [0];

 

The iterator uses the auto-increment operator to move the iterator forward to the next element in the container. Since the iterator returned by the end operation does not point to any element, it cannot be unreferenced or auto-increment.

 

4 const_iterator

Each container type also defines a type named const_iterator, which can only be used to read elements in the container, but cannot change its value.

When we dereference a common iterator type, we get a non-const reference to an element. If we dereference the const_iterator type, we can get a reference pointing to the const object. Like any constant, this object cannot be overwritten.

 

5. Arithmetic Operations of the iterator

The vector iterator also supports other algorithm operations. These operations are called iterator arithmetic operations.

① ITER + N; ITER-N; an integer value can be added or subtracted to the iterator object. In this way, a new iterator is generated, which is located before or after the element indicated by ITER. The result after addition or subtraction must point to an element in the vector indicated by ITER, or the last element at the end of the vector. The size_type or difference_type of the vector is the type of the value that is subtracted from the fire.

② Iter1-iter2; this expression is used to calculate the distance between two iterator objects. The distance is a signed value named difference_type. The difference_type here is similar to the size_type type.

========================================================== ========================================================== ======================================

A QQ Group created by myself! This article mainly discusses Linux C language, shell script, and driver development. You are welcome to join us. First of all, I declare that my skills are good .... Haha
QQ: 213622826

========================================================== ========================================================== ======================================

 

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.