The traversal of the string series--string in combat C + + (using subscript or iterator)

Source: Internet
Author: User

Iterators provide a way to access objects in the container. For example, you can use a pair of iterators to specify a range of objects in a list or vector. An iterator is like a pointer. In fact, C + + pointers are also an iterator. However, iterators can also be class objects that define the operator* () and other operator-like methods that are similar to pointers.

We all know that you can use the subscript operation to access a string object and a vector object. There is also a more general approach that can be implemented. Name: Iterator (iterator).

Like pointers, Iterators also provide indirect access to objects. In the case of iterators, the object is an element in a container or a character in a string. An iterator can be used to access an element, and an iterator can move from one element to another. Iterators have valid and invalid points, and a valid iterator points to an element or to the next position of the tail element in the container. All other conditions are invalid.

Unlike pointers, iterators do not use the accessor, but instead use the begin and end members.
Combining dereference and member access operations

A dereference iterator can get the object that the iterator refers to, and if the object's type is exactly a class, then the members of the class can be accessed. For example:

(*it). empty ();

Note here that *it must be parenthesized, otherwise it will go wrong. If not, the meaning of this sentence becomes an empty member of the access it, but it is an iterator with no empty members. The arrow operator-> is provided in c++11, and the arrow operator combines the dereference with the member access two operations. So Iter->mem is equivalent to (*iter). Mem.

Note: Once you have used the iterator's loop body, do not add elements to the container to which the iterator belongs.

Arithmetic operations of Iterators:

The ITER + N iterator is still an iterator after the addition of an integer, where iterators and pointers resemble each other and can be interpreted as an addition and subtraction of addresses.

Iter1-iter2 The result of subtracting two iterators is the distance between them. That is, the distance to the pointing position.

The <,<=,>,>= iterator relational operator, which is smaller than the container position that the iterator points to before another iterator refers to the position.

==============================================================
Why does the string vector use subscript access and also design an iterator pattern?

Personally feel:
1, STL set the original intention is that the algorithm container separation
2, Iterators more general, and some container objects do not support subscript
3, some methods, such as erase can only pass iterator

The vector is not very different, the iterator is actually the original pointer, but in the case of C + + as a whole container or iterator good, such as list of these containers, and so on, the iterator provides you with a middle layer, abstract out the differences between the containers, so that you can access the container in the same way

The traversal of the string series--string in combat C + + (using subscript or iterator)

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.