[STL] C ++ iterator interator

Source: Internet
Author: User

Containers are generic data structures, while iterators are generic pointers that can point to elements. A container is equivalent to a storage cabinet, where many different items are like storage elements, such as bread, beer, apple, and cash. To get each object, you must use a tool that matches each object. For example, you must take out the bread and use a plate, take out the beer and use a cup, take out the apple and use a basket, and take out the cash and use a wallet. The role of the iterator is equivalent to the abstraction of the tool for retrieving items. The iterator refers to the tool for retrieving objects from the storage room in real life. The C ++ iterator is a data type that checks elements in a container and traverses elements.
The C ++ iterator interator is a pointer to a certain STL object. This pointer allows you to easily traverse all elements.
1. iterator Definition
Iterator in C ++ is an important concept in STL. The concept of iterator is derived from the abstraction of traversing a linear container tool, that is, how you can access an element of the container. For the simplest array, you can use the index value of the array, because the array is continuously stored in the memory, but for the linked list, you must use a pointer. In addition, there are also many data structures that require a convenient tool to access the elements, methods such as ID and keywords. To unify the use of such tools for all containers, developers who provide a complete set of containers generally use a method to express the access tools for various containers. For example, C ++ STL uses iterator. Use Position for the MFC container. C # and Java have their own methods, but the methods remain unchanged.
The usage of iterator can be unified, but the principle of iterator implemented by different underlying containers is different. For example, iterator ++ can be understood as moving to the next element of the container. If the underlying layer is an array, you can add one to the index value. If the underlying layer is a linked list, you have to perform operations similar to m_pcurrent = m_pcurrent-> pnext. Therefore, each container has its own iterator implementation method.
Common iterator methods include:
Iterator ++ move to the next element
Iterator -- move to the previous Element
* Iterator accesses the value of the element referred to by iterator
<>==! = Comparison between iterator, for example, determining which element is in front
2. Container iterator type
Each container type defines its own C ++ iterator type, such as vector: vector <int>: iterator ITER. This statement defines a variable named ITER, its data type is the iterator type defined by vector <int>. Each standard library container type defines a member named iterator. The iterator here has the same meaning as the actual type of the iterator.
Begin and end operations each container defines a pair of functions 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>: iterator iter = ivec. Begin ();
The preceding statement initializes ITER to the value returned by the vector operation. Assume that the vector is not empty. After initialization, ITER indicates that the element is ivec [0].
The C ++ iterator returned by the end operation points to the "next end element" of the vector ". Off-the-end iterator ). Indicates that it points to a nonexistent element. If the vector is empty, the iterator returned by begin is the same as the iterator returned by end. The iterator returned by the end operation does not point
Any actual element in the vector. On the contrary, it only acts as a sentinel, indicating that we have processed all the elements in the vector.

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.