C + + vector (08)

Source: Internet
Author: User

The size of the array will not change over the lifetime of the array. A vector is a class version of a one-dimensional array, which is similar to an array where element items are always stored continuously, but it is different from arrays: how much of the stored elements in a vector can grow or shrink dynamically as needed in the run. A vector is a class template that has member functions.

The vector class template is defined in the header file vector, which provides 4 constructors to define a list of elements. The length is expressed as the size, and the data type is in type and the object name is name.

Vector<type>:: iterator generic pointer name;

Methods of accessing vector capacity information

(1) Size (): Returns the number of objects already stored in the current vector

(2) Max_size (): The return vector can hold the maximum number of objects, which is generally the number of objects to be accommodated by the operating system's addressing space. This parameter is not user-specified, it depends on the hardware structure.

(3) Capacity (): Returns the number of objects that can be accommodated without having to allocate memory again. Its initial value is the number of elements the programmer originally requested. When the storage space is full and an element is added, it automatically doubles the expansion space on the original basis to store more elements. In layman's words, it is the space that has been applied. The relationship between the three is as follows.

(4) Empty (): Returns True if the current vector is empty

Methods for accessing objects in vectors

(1) Front (): Returns the first object in a vector.

(2) Back (): Returns the last object in the vector.

(3) operator[] (size_type,n): Returns the first N+1 object in the vector (a vector element with the subscript n).

Methods for inserting objects in vectors

(1) push_back (const t&): Inserts an object into the tail of the vector.

(2) Insert (iterator It,const t&): Inserts an object before it points to the vector position.

(3) Insert (iterator It,size_type n,const t&x): Insert an N value X object before it points to the position.

Methods for deleting objects in vectors

(1) pop_back (const t&): Deletes the last object in the vector.

(2) Erase (iterator it): Removes the container object that it points to.

(3) Clear (): Delete all objects in the vector, empty () returns True.

C + + vector (08)

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.