C ++ Study Notes (10): iterator and Study Notes Generator
We all know that we can use subscript operations to access string objects and vector objects. There is also a more general method that can be implemented. The name is iterator ).
Similar to pointers, The iterator also provides indirect access to objects. As far as the iterator is concerned, its object is a character in the element or string in the container. You can use the iterator to access an element, and the iterator can also move from one element to another. The effective iterator points to the next position of an element or the tail element in the container. Other cases are invalid.
Unlike pointers, The iterator uses the begin and end members instead of the accessors.
As follows:
Auto a = v. begin (), B = v. end ();
Here, a represents the first element of v, and B represents the element at the next position of the last element of v.
Note: The end member returns the iterator pointing to the "next position of the tail element" of the container. That is to say, the content indicated by this iterator is an element that does not exist at all. It is also called the end iterator. This iterator has no actual meaning. It is just a tag, indicating that we have processed all the elements in the container. If the container is empty, begin and end return the same iterator.
In general, we may not know the type of the iterator, so use the auto keyword to define the variable. The above example uses this method.
Iterator OPERATOR:
* Iter returns the reference of the element pointed to by the iterator.
Iter-> mem gets the member of the element. Equivalent to (* iter). mem
++ Iter points to the next element of the container
-- Iter points to the previous element of the container.
= And! = If the two iterators point to the same element, and the latter points to the last iterator of the same container, the same
Similar to pointers, The iterator can also obtain the elements it indicates by referencing the iterator. The iterator that executes the unreferenced operation must be legal and indeed indicate an element. It is illegal to reference an invalid iterator or post-iteration.
The following is an example:
String s ("good job ")
If (s. begin ()! = S. end () {// ensure that S is a non-null string
Auto it = s. begin (); // it indicates the first character of s
* It = toupper (* it) // change the first character of the string to uppercase, which must be referenced here; otherwise, the operation fails.
}
Combined with disreference and member access operations
The dereference iterator can obtain the object referred to by the iterator. If the object type happens to be a class, the member of the class can be accessed. For example:
(* It). enpty ();
Note that * it must be enclosed in parentheses; otherwise, errors may occur. If this clause is not added, it means to access the it empty member, but it is an iterator without empty members. In C ++ 11, the arrow operator> is provided. The Arrow operator combines the unreferenced and member access operations. Therefore, iter-> mem is equivalent to (* iter). mem.
Note: Once the iterator body is used, do not add elements to the container to which the iterator belongs.
Arithmetic Operations of the iterator:
The iter + n iterator is still an iterator after an integer is added. Here, the iterator is similar to the pointer and can be understood as the addition and subtraction on the address.
Iter1-iter2 the result of the subtraction between the two iterators is the distance between them. That is, the distance to the position.
<, <=,>,> = Iterator relational operator. If an iterator points to a container before the position indicated by another iterator, the former is smaller than the latter.