One of the common containers of "C + +" STL: Container and iterator __c++

Source: Internet
Author: User
Tags data structures
statement:1, Ben Boven mainly from the "C + + Primer" and "STL Source analysis" These two classic books. At the same time, also referred to the network of many excellent blogs, the authors of these blogs thanked. 2, due to the limited ability of bloggers, the use of some containers may not have been in-depth study. Therefore, if there are errors and deficiencies in this blog, you are welcome to criticize. 3, Ben Boven only for academic exchanges only use, without any other uses.

To get to the bottom of this, let's start with the STL container in C + +. 1, the concept of containers

Standard Template Library, Standard Template gallery.
The so-called STL container, is the most commonly used data structure to achieve. The most commonly used data structures are array arrays, list lists, tree trees, stack stacks, queue queues, hash tables, set sets, map maps, and so on, according to the "data in the container arrangement" characteristics, these data structure can be divided into two types of sequence and correlation. As a result, containers may also be divided into sequential containers and associated containers of two.
2. Introduction to Iterators

The iterator is an integral part of the container when we use it. The iterator is used in STL to connect the algorithm with the container, which plays a role as a binder. An iterator is a data type that examines elements within a container and traverses the elements. An iterator is an object that behaves like a pointer, providing a pointer-like function to visit the contents of a container, while the most common and most important of the various actions of a pointer is "content extraction" and "member access." Therefore, the most important programming work of iterators is to overload the operator* and operator->. 1, the type of iterator of the container

Almost all of the algorithms provided by STL work by using an iterator to access the sequence of elements, each of which defines its own specific iterator type to access elements in the container, such as vectors:

Vector<int>::iterator iter;  A vector iterator that declares ITER to be of type int

This statement defines a variable named ITER, whose data type is the iterator type defined by the vector. Each standard library container type defines a member named iterator, where the iterator is the same as the actual type of the iterator. The term iterator usually refers to a conceptual iterator, whereas the term iterator type is a specific iterator type that has a container definition, such as a vector.
Common types of iterators are: iterator type iterator, read-only iterator type of element const_iterator, iterator for addressing elements in reverse order reverse_iterator, read-only reverse iterators for elements Const_reverse_ Iterator. 2. The Begin and end operations of the iterator

Vector<int>::iterator iter = Vec.begin ();

The iterator returned by begin points to the first element within the container, and the iterator returned by end points to the "next position in the last element" within the container, and the iterator returned by this operation does not point to any actual element in the vector, it just acts as a sentinel, Indicates that we have finished processing all the elements in the vector.
Rbegin returns a reverse-order iterator that points to the last element of the container, Rend returns an iterator that points to the position in front of the first element of the container. 3. Self-increasing and reconciliation references for iterators

The iterator type can use the dereference operator (the * operator) to access the element that the iterator points to, and the dereference operator returns the element that the iterator is currently pointing to. However, you cannot dereference the iterator returned by the end operation. Iterators can also use the self-increment operator to move the iterator forward to the next element in the container, which is similar in form to the self augmentation of an int object.

Relational operators apply only to vector and deque containers, because only these two containers provide fast, random access to their elements. They ensure that the specified container element can be accessed directly and efficiently based on the element location. Both of these containers support random access through the location of the elements, so their iterators can effectively implement arithmetic and relational operations.
The C + + language uses a pair of iterators to mark the scope of the iterator. Iterator scope: [First, Last] is a left-closed interval that indicates the range starts at the beginning, ends at last, but does not include the last, which is the next element to the final element in the container. Note: If first is not equal to last, a repeat of the self increment for first must be able to reach last, otherwise, if the last is before first, undefined behavior will occur. The scope of the iterator uses the meaning of left closure: because it can represent the empty set uniformly, there is no need for special handling. When the primary and last are equal, the scope of the iterator is empty. 4, each container supports the iterator category

Only sequential containers and associative containers support iterator traversal, and the types of iterators supported by each container are as follows:

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.