Generic programming and stl--scenarios for the failure of various container iterators

Source: Internet
Author: User

scenarios where various container iterators fail:

In fact, when defining an iterator invalidation: After some operations are completed, the iterator is considered to have a change in the value it points to, or the iterator points directly to an illegal space, which is assumed to be invalid. The iterator is considered invalid as long as it is not a point before the operation.

When placing elements in a vector, the difference between size and capacity becomes particularly important. If the size of the vector equals its capacity, the only way to place a new element is to increase the amount of memory in the vector, which means allocating a new and larger memory, copying the contents of the old memory block into the new block of memory, and then returning the old memory block.

Once the vector's memory is redistributed, its iterators is invalidated. In addition, inserting or deleting elements in the middle of a vector also points to the failure of all iterators of the element after the placement point or the delete point. This means that if you originally allocated memory with the member function reserve, or if all the placement or deletion actions were performed at the end of the vector, you could make the iterator of the vector non-invalidated.

At the same time, there are copy constructors and assignment constructors within the vector.

Vector<int> TEST_WW (VECC); Using the copy constructor

Vector<int> TEST_WW;

TEST_WW =vecc;//is called an assignment constructor

For list:

is a doubly linked list that allows you to place and remove elements at the start, end, and middle of a list with O (1) time. List has an important mind, placement in the binding (splice) action will not cause the iterators to the list to fail, even if the deletion of the action, will only point to the deleted element of the iterator invalid. The order of these iterators may be changed (that is, after a list operation, a list<t>::iterator may have different predecessor and successor elements), but iterators itself is not invalidated or therefore points to different elements, Unless you understand the execution of an invalid action or object transformation action.

Suppose I is a valid iterator of type vector<t>::iterator, when we place or remove an element in a position before I, I point to different elements, or I completely fail at all. On the other hand, assuming that I and J are both iterators that point to the vector, there is an integer n that makes i = = j+n. In this case, even if the element is placed in a vector and I and J point to different elements, the relationship between the two pointers remains unchanged. The list is the opposite, its iterators will not be invalidated, nor will it point to different elements, but the relationship between the Iterators's predecessor and the successor element will not remain unchanged.

Deque Instructions for use:

Deque is similar to a vector, where the element is placed or removed at the end of a constant event, and the element is inserted or removed in the middle of a linear event.

The difference between deque and vectors is that deque also provides a constant event to add or remove elements from the beginning of the sequence. To place an element at the beginning or end of a deque, the time of O (1) is required, and the complexity of the element being placed in the middle is linearly related to N, where n is the shortest distance from the end of the deque opening of the placement point.

Another difference is that deque does not have memberfunctions such as vector-like capacity () and reserve (), nor does it provide any functions validity guarantees related to member iterator. Instead, it guarantees that "placing elements at the beginning or end of a deque" does not cause any existing elements in the deque to be duplicated. For deque, the behavior of a vector-like memory redistribution does not occur.

In general, insert (including Push_front and push_back) causes all iterators that point to deque to fail. Calling Erase in deque also causes all iterators that point to deque to fail. Calling erase (including Pop_front and Pop_back) to the beginning or end of the deque will cause the iterator of the removed element to fail.

The deque is implemented in a dynamically-segmented array. Deque typically contains a header that points to a set of nodes (nodes), each containing a fixed number of elements that are continuously stored. When the deque grows, it adds new nodes.

For deque and list, the interior has push_back pop_back () Push_front () Pop_front ()

But for vectors, only the Pop_back () of Push_back ()

For set:

Like List, set has several important properties: the placement of the new element does not invalidate the iterators of the existing element, and removing the element from the set does not invalidate any iterators----of course, except for the iterator of the deleted element. Because set is implemented with red and black trees, the interior is also in the form of nodes.

For map:

Like list, map has an important property: the placement of new elements does not invalidate the iterators of existing elements. Deleting an element from the map does not cause any iterators to fail---except for the iterator of the element being deleted.

For Multiset:

Like List, Multiset has several important properties: the placement of the new element does not invalidate the iterators of the existing element, and removing the element from the set does not invalidate any iterators-except the iterator of the element being removed.

For Multimap:

As with list: Multimap has an important property: the placement of new elements will not cause the iterators of the existing elements to fail. Deleting an element from Multimap does not cause any iterators to fail-except for the iterator of the element being removed.

For priority_queue:

It is an adapter that is implemented at the bottom of a container. The default level is vector, but you can also specify different levels of the type. Its bottom storage element is vector-implemented. But the queue with priority is the standard data structure, which can be implemented in different ways. Usually priority_queue are implemented in heap, and Make_heap, Push_heap, and pop_heap are used to maintain the state of the heap. This means that the priority queue is implemented with heaps.

For generic programming, which is the use of templates, but the use of iterators in the STL as a binder, so that the container and algorithm separation, so that the STL is better extended, the template is a polymorphic, static polymorphism, that is, the polymorphism during compilation, during compilation instantiation. But the virtual mechanism is run-time polymorphic. It is not known when it will be called during the run time.

"Generic Programming and STL" This book describes the use of generic programming in the STL, what generic programming, from the basic programming of C + + to achieve their own needs, and then update the technique according to the requirements, the concept of the iterator, the concept of the iterator around the expansion, the type of the iterator is described, The properties of various iterators are analyzed theoretically. Then the importance of the algorithm is described, and the iterator introduced above is introduced into the algorithm. The concept of the container is introduced, which makes the iterator a binder between the container and the algorithm.

Define external interfaces in the implementation of each container, such as the categories of various iterators and iterators, so that the algorithm points to them based on these attributes, including the partial specificity of some container elements. (The difference between specificity and specificity?) For example, an algorithm implementation template has been defined, but for pointers to the top of a template, this is biased, but in order to int* this pointer to define a template is special. To redefine a template (such as a pointer), but to redefine a template (such as the int* type) for a particular class, there are many biases in the STL

For vectors:

member functions have Pop_back () push_back ()

For list deque:

member functions have Pop_front () Push_front () push_back () Pop_back ()

For slist:

member functions have Push_front () Pop_front ()

At the same time, these containers also have the Insert () function

However, the insertion of these containers and associated containers is only an insert () function

Generic programming and stl--scenarios for the failure of various container 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.