A preliminary study of STL Library

Source: Internet
Author: User

Library: No errors, smaller than frames, to solve small problems.

Framework: A full-featured solution in development.

STL: Standard specification, code different, style, frame difference

Including:

Algorithm

Container

STL includes:

    1. Container: The structure that holds the data

    2. Iterators: Adhesives that act as containers and algorithms

    3. Spatial Configurator: allocation and management of memory space (dynamic allocation)

    4. Adapter: Something that modifies a container: stack,queue,eg: Reverse iterator

      More constrained, does not support traversal

    5. Algorithm #include<algorithm>

    6. Imitation functions


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/84/98/wKiom1eVvdrhGjCZAAE0bX_qMvM365.png "title=" QQ picture 20160725152007.png "alt=" Wkiom1evvdrhgjczaae0bx_qmvm365.png "/>

Iterator: Point to an iterator, like a pointer (a type-encapsulated pointer)

Iterator invalidation: Similar to a wild pointer, and delete about

Erase parameters

1. One position (iterator)

2. Position area (iterator)


An iterator (iterator) is an object that can perform a pointer-like operation, such as dereferencing (operator* ()) and incrementing (operator++ ()), which we can understand as a pointer. But it is not our so-called normal pointer, we can call it a generalized pointer, you can see by sizeof (Vector::iterator), the memory is not 4 bytes.


First, for vectors, the add and delete operations may invalidate some or all of the iterators in the container. Then why does the iterator fail? Vector elements are stored sequentially in memory, imagine: if 10 elements already exist in the current container, and now add an element to the container, but in memory immediately after the 10 elements there is no free space, and vector elements must be stored in order to index access, So we can't find a place to store this element anywhere in memory. The vector must then reallocate the storage space to hold the original elements and the newly added elements: the elements stored in the old storage space are copied into the new storage space, the new elements are inserted, and the old storage space is finally revoked. This happens and will cause all iterators in the vector container to fail.


We see the realization of the above-mentioned allocation and revocation of memory space in order to achieve the vector's self-growth, efficiency is extremely low. In order for the vector container to achieve fast memory allocation, the actual allocation of containers will be more than the current space required, the vector container reserved These additional storage, to hold the newly added elements, and do not need to reallocate new storage space each time. You can see this mechanism by implementing capacity and reserve members from a vector.


capacity and size differences: size is the number of elements that the container currently owns, while capacity refers to the total number of elements that the container can store before it must allocate new storage space.


Several failure cases of vector iterators: 1. When an element is inserted (push_back), the iterator returned by the end operation is definitely invalidated. 2. When an element is inserted (push_back), the capacity return value changes before the element is inserted, and the entire container needs to be reloaded, at which time the iterator returned by the first and end operations will be invalidated. 3. After the delete operation (Erase,pop_back), the iterator to the delete point is all invalidated, and the iterator to the element that follows the delete point will all be invalidated.


Deque iterator failure condition: In the C++primer book is this limit: 1. Inserting elements in the Deque container header or tail does not invalidate any iterators. 2. Deleting an element at its first or trailing end only invalidates the iterator that points to the deleted element. 3. The insert and delete operation at any other location in the Deque container invalidates all iterators that point to the container element

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1829674

A preliminary study of STL Library

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.