C ++ standard library vector container

Source: Internet
Author: User

We can regard vector as an array upgrade. It is an array in a broad sense and its data type is defined by itself. The number of array elements is unlimited.

Vector definition: vector <type> variable name

Initialization Method:

Vector <t> V1 // The default constructor V1 is empty.
Vector <t> V2 (V1) // V2 becomes a copy of V1
Vector <t> V3 (n, I) // V3 contains N I Elements
Vector <t> V4 (n) // V4 contains a copy of the initialized Element

In the third initialization method, a vector object is determined by the number of elements and element values. For a vector object, the goal is to dynamically allocate memory and add elements instead of pre-determine the memory size.

Operations on a vector object

V. Empty () // If V is null, true is returned; otherwise, false is returned.
V. Size () // returns the current number of elements of v.
V. push_back () // insert an element at the end of V
V [N] // access the nth element in V
V1 = v2 // replace element V1 with element v2
= ,! =, <, <=, >=,> // Reserved meaning

The return value of V. Size () similar to string is of the size_type type.

The following table can be used to access a vector object: vector <int> KK; kk [N]

For (vector <int >:: size_type num = 0; num! = 10; num ++)

Yes! = Is safe as the condition, which is different from C ++ and C, and does not take the condition of preventing overflow <=

However, the subscript indicates that [] can only extract existing elements (that is, it can be understood that cross-border access is not allowed in the array), especially when a vector object is just defined, the number of elements of the default object is 0, and an error occurs during subscript access.

C ++ProgramAll members use the iterator as a tool to access the container.

Vector <t>: iterator V1;

Each container has the begin and end functions to return the iterator. The begin function returns the iterator pointing to the first element, the end function returns the iterator next to the last element.

The anchor operator (*) can be used to access the elements pointed to by the iterator, And the iterator has the auto-adding function, that is, pointing to the next position in the container, the Return Value of the end function indicates that * cannot be used to access the return value of the end function.

Vector <t> KK;

For (vector <t>: iterator K = KK. Begin (); k! = KK. End (); k ++)

Traverse the entire container so that it is used here! = Is also safe (in fact, we can regard the iterator as a pointer, a class of pointers used to traverse the vector container)

Function

Statement

C. Assign (beg, end)

C. Assign (n, ELEM)

Assign the value of the data in the [beg; End) interval to C.

Assign a copy of n elem values to C.

C. At (idx)

Returns the data indicated by the index idx. If idx is out of bounds, out_of_range is thrown.

C. Back ()

Returns the last data and does not check whether the data exists.

C. Begin ()

Return the unique data of the iterator.

C. Capacity ()

Returns the number of data in the container.

C. Clear ()

Remove all data from the container.

C. Empty ()

Determines whether the container is empty.

C. End ()

Point to the last data address in the iterator.

C. Erase (POS)

C. Erase (beg, end)

Delete the data at the POs position and return the location of the next data.

Delete the data in the [beg, end) interval and return the location of the next data.

C. Front ()

Return a data record.

Get_allocator

Returns a copy using the constructor.

C. insert (Pos, ELEM)

C. insert (Pos, N, ELEM)

C. insert (Pos, beg, end)

Insert an ELEM copy at the POs position and return the new data location.

Insert n ELEM data at the POs position. No return value.

Data inserted in the [beg, end) range at the POs position. No return value.

C. max_size ()

Returns the maximum number of data in the container.

C. pop_back ()

Delete the last data.

C. push_back (ELEM)

Add a data entry to the end.

C. rbegin ()

Returns the first data of a reverse queue.

C. rend ()

Returns the next location of the last data in a reverse queue.

C. Resize (Num)

Specify the length of the queue again.

C. Reserve ()

Reserve the appropriate capacity.

C. Size ()

Returns the actual number of data in the container.

C1.swap (C2)

Swap (C1, C2)

SWAps C1 and C2 elements.

Same as above.

Vector <ELEM> C

Vector <ELEM> C1 (C2)

Vector <ELEM> C (n)

Vector <ELEM> C (n, ELEM)

Vector <ELEM> C (beg, end)

C .~ Vector <ELEM> ()

Create an empty vector.

Copy a vector.

Create a Vector Containing N pieces of data, which are generated by default.

Create a Vector Containing n elem copies.

Create a vector in the [beg; End) interval.

Destroys all data and releases memory.

Related Article

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.