5_stl Design Concept _ iterators

Source: Internet
Author: User

The stone of his mountain, can attack Jade.

Http://blog.csdn.net/jxh_123/article/details/30793397?utm_source=tuicool&utm_medium=referral

Focus:

1, iterator iterator is a smart pointer , it encapsulates the original pointer, and provides some equivalent to the original pointer operation , to be both convenient and secure .

2 . Iterators are an important bridge for connecting containers and algorithms.

Function prototypes for the Find function are: Template<class _init,class _ty> _init Find (_init _first, _init _last, const _ty & _val)

From the Find function prototype, we can see that the Find function is a function template, the first two parameters of the function are _init, the parameter is the abbreviation of Inputiterator, and the last parameter is a reference to an arbitrary type variable.

When we use the Find function, the argument passed in is find (Vec.begin (), Vec.end (), 7).

this way our parametric iterators link the algorithm find to the container vector, and from that point of view we can easily understand why the iterator is the bridge between the algorithm and the container .

Reason: You can use a derived class object as an argument to pass a function to the base class type as a formal parameter, so the Vec.begin () in the Find function is the argument, and the input iterator type as the formal parameter, the two can achieve the purpose of combining the actual situation without error.

As a result, iterators provide a platform for communication between various algorithms and various types of containers.

Actions that cause an iterator to fail:

I. operation that invalidates the vector iterator

Analysis: Vector is a dynamic array, after which the push_back is shifted back one, resulting in the failure of the iterator pointing to the subsequent element;

C + + for vector pre-allocation of a portion of space, if the insertion operation caused insufficient space, the additional application for a large space (usually twice times the original space), and the original elements to the new space, destruction of space.

1. Adding elements to the vector container (push_back,insert)

Add elements to the vector container in the following two scenarios:

1) If the element is added to the vector, the entire vector is reloaded (that is, re-requesting space), that is, the return value of the capacity () of the vector of the previous and the two vectors is not the same, at which point the iterator of all elements in the container will be invalidated.

2) If the addition does not cause the entire vector container to load, then the iterators that point to those elements that follow the newly inserted element will be invalidated.

2, delete operation (erase,pop_back,clear)

After a vector performs a delete operation, the iterator that corresponds to the deleted element and the iterator that corresponds to its subsequent element are invalidated.

3.resize Action: Adjust the size of the current container

The effect of resizing the container on the iterator is discussed in the following scenarios:

A. If the size>capacity is adjusted, it will cause the entire container to reload and the entire container's iterator will be invalidated .

B. If the size<capacity is adjusted, it will not reload, as follows:

B1. If the size of the container is adjusted before the size> of the container , all iterators of the original vector will not be invalidated ;

B2. If the size of the container is adjusted after the size< of the container, the iterator that corresponds to the cut-off element in the container will be invalidated .

4. Assignment operation (v1=v2 v1.assign (v2))

All iterators that result in the left operand v1 are invalidated , and the iterator to the right operand v2 is obviously not invalidated .

5. Switching operation (V1.swap (v2))

Because V1,v2 does not delete or insert elements during an exchange operation, no elements are moved inside the container, so all iterators of the v1,v2 are not invalidated .

Second, the operation that invalidates the Deque iterator

Analysis: The deque is a double-ended array, and the end-of insertion is extended outward, and the middle element remains unchanged. Inserting internally using insert causes the iterator to become invalid.

1. Insert operation (Push_front,push_back,insert)

The effect of an deque insert operation on an iterator is divided into two scenarios:

A. Inserting elements in the Deque container header or tail is not invalidated by any iterators;

B. Inserting an element at a different location from the end of the tail invalidates all iterators of that container.

2. Delete operation

A. Deleting an element in the deque first and tail only invalidates the iterator corresponding to the deleted element;

B. A delete operation at any other location invalidates the entire iterator.

Third, the operation that invalidates the List/map/set iterator

Since the elements in the List/map/set container are connected by pointers, the data structure of the list implementation is a doubly linked list, and the data structure implemented by Map/set is a red-black tree, so the insertions and deletions of these containers simply change the pointer's direction, not the elements inside the container. Therefore, the addition of elements within the container does not invalidate any iterators .

When you delete an element, it simply invalidates the pointer to the deleted iterator .

When using map, note: The Erase function of map in STL is returned as void, unlike the sequential container, which returns the next element of the deleted element

if (*it = = value)   map.erase (it+ +);   // using the properties of Post + + Else    ++it;

The i++ return value is a temporary variable that overloads when the parameter uses a dummy int to separate from the ++i to achieve the purpose of overloading;

member function method (implied this pointer): Post + + + Reload

int operator+ + (int)     {int tem =* This;      ++*this;     Rerurn tem;}        

For sequential containers you can:

if (*it = = value   ) = Vector.erase (it);   // Sequential Container Delete operation that returns the iterator position of the next element of the deleted element Else    ++it;

5_stl Design Concept _ iterators

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.