One of the STL containers vector

Source: Internet
Author: User

One of the simplest and most useful containers in STL is the Vector<t> class template, called the vector container, which is one of the sequence type containers.

Container capacity can be modified selectively.
(1) Statement:
Vector<type> v; A capacity of 0 constructs a V object that specifies the element type
Vector<type> v (n); The capacity is n to construct a V object, specifying the element type
Vector<type> V (n, InitValue); The capacity is N construction v object, specifying the element type as type, and all elements are initialized to InitValue;

(2) member functions
V.capacity (); Returns v to store the number of elements before it expands
V.size (); Returns the number of elements (values) currently contained in V, that is, the length of V
V.empty (); Null function if and only if V does not contain any elements (values) that are returned true
V.reserve (n); Grow v so that it has a capacity of N and does not affect the length of V (the number of actual storage worth)
V.push_back (value); The value is added at the tail of V and the length of V is +1
V.pop_back (); Turn out the last element of V and set the length of V-1
V.front (); Returns a reference to the first element of V
V.back (); Returns a reference to the last element of V

The length of V is the number of all data items in the vector object, i.e. v.size ();
The capacity of V is the number of elements that can be stored in a vector object, i.e. v.capacity ();

(3) Subscript operation for Vector objects
V[i]; To access the elements in the V below Table I
V1 = v2; Assign a copy of V2 to V1
V1 = = V2; Returns True when and only if V1 and V2 have the same given value in the same position, that is, the comparison of one element of an element
V1 < v2; Returns True when and only if V1 is less than V2 on the dictionary sort

V can be accessed using subscript and the following table operator operators;
The following table for the first element of V is: 0;
The following table for the last element of V is: v.size ()-1;
The following table of the vector object is mined with the string object and the C-style array below the table operation, but there is also an important difference that the vector object cannot be directly

Increment of the following table operation, and the capacity of V is not specified, or less than the value of the table below, cannot update the capacity of the vector object by the following table operation, such a situation must use

V.push_back (Value) to handle.

(4) Include iterator vector<t> member function
STL Containers basically support an iterator's access method. In essence, "iteration" is a special type of object that can be passed through the storage element

The address "points to" the elements in the container, such as the Vector<t> class container, and the ability to access the values stored at that location and the ability to move from one element to another. Every

An STL container provides a set of its own iterator types and (at least) two ways to return iterators: *begin (): Returns an array of elements positioned in the first element of the container.

Iterator, *end (): Returns an iterator positioned immediately after the last element of the container, next to the element, which actually indicates the data in the container, two member functions

A "Half open area" with two benefits (1) using the iterator's value equals end to represent the end of the iteration, (2) It is convenient to use the BEGIN = = end to determine if the container is

No is empty.

Iterator member functions for vector<t> objects
V.begin (); Above
V.end (); Above
V.rbegin (); Returns the last worthwhile reverse iterator positioned at V
V.rend (); Returns an iterator positioned in front of the first value of V, immediately next to the transformation
V.insert (pos, value); Say value is inserted at the iterator position at V Pos
V.insert (POS, n, value); The N-copy value of value is inserted at the position Pos of the iterator at V
V.erase (POS); Remove the value at the position pos of the iterator at V, the length of V -1,v the capacity of the
V.erase (POS1, Pos2); Remove value from iterator position pos1 to Pos2 in V

Initialization of iterators:vector<t>:: Iterator it = initial value;
Three important actions for iterators:
# it++; Move it forward to the next element of the vector
# it--; Move it backwards to the previous element of the vector
# *it; Accessing the value of an iterator it refers to an element

One of the STL containers vector

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.