C++ Library - <vector>

Vectors are sequence container that can change size. Container is a objects that hold data of same type. Sequence containers store elements strictly in linear sequence.
Vector stores elements in contiguous memory locations and enables direct access to any element using subscript operator []. Unlike array, vector can shrink or expand as needed at run time. The storage of the vector is handled automatically.
To support shrink and expand functionality at runtime, vector container may allocate some extra storage to accommodate for possible growth thus container have actual capacity greater than the size. Therefore, compared to array, vector consumes more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.
Zero sized vectors are also valid. In that case vector.begin() and vector.end() points to same location. But behavior of calling front() or back() is undefined.
Below is definition of std::vector from <vector> header file
template < class T, class Alloc = allocator<T> > class vector;

T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
Alloc − Type of allocator object.
By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Alloc − Type of allocator object.
By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Following member types can be used as parameters or return type by member functions.
Below is list of all methods from <vector> header.
Constructs an empty container, with zero elements.
Constructs a container with n elements and assignd val to each element.
Constructs a container with as many elements in range of first to last.
Constructs a container with copy of each elements present in existing container x.
Constructs the container with the contents of other using move semantics.
Constructs a container from initializer list.
Destroys container by deallocating container memory.
Assign new values to the vector elements by replacing old ones.
Assign new values to the vector elements by replacing old ones.
Assign new values to the vector elements by replacing old ones.
Returns reference to the element present at location n in the vector.
Returns a reference to the last element of the vector.
Return a random access iterator pointing to the first element of the vector.
Returns the size of allocate storage, expressed in terms of elements.
Returns a constant random access iterator which points to the beginning of the vector.
Returns a constant random access iterator which points to the beginning of the vector.
Destroys the vector by removing all elements from the vector and sets size of vector to zero.
Returns a constant reverse iterator which points to the reverser beginning of the container.
Returns a constant reverse iterator which points to the reverse end of the vector.
Returns a pointer to the first element of the vector container.
Extends container by inserting new element at position.
Inserts new element at the end of vector.
Tests whether vector is empty or not.
Returns an iterator which points to past-the-end element in the vector container.
Removes single element from the the vector.
Removes single element from the the vector.
Returns a reference to the first element of the vector.
Returns an allocator associated with vector.
Extends iterator by inserting new element at position.
Extends vector by inserting new element in the container.
Extends vector by inserting new element in the container.
Extends vector by inserting new element in the container.
Extends vector by inserting new element in the container.
Returns the maximum number of elements can be held by vector.
Assign new contents to the vector by replacing old ones and modifies size if necessary.
Assign new contents to the vector by replacing old ones and modifies size if necessary.
Assign new contents to the vector by replacing old ones and modifies size if necessary.
Returns a reference to the element present at location n.
Removes last element from vector and reduces size of vector by one.
Inserts new element at the end of vector and increases size of vector by one.
Returns a reverse iterator which points to the last element of the vector.
Returns a reverse iterator which points to the reverse end of the vector.
Requests to reserve vector capacity be at least enough to contain n elements.
Changes the size of vector.
Requests the container to reduce it's capacity to fit its size.
Returns the number of elements present in the vector.
Exchanges the content of vector with contents of vector x.
Tests whether two vectors are equal or not.
Tests whether two vectors are equal or not.
Tests whether first vector is less than other or not.
Tests whether first vector is less than or equal to other or not.
Tests whether first vector is greater than other or not.
Tests whether first vector is greater than or equal to other or not.
Exchanges the contents of two vector.
 Print
 Add Notes
 Bookmark this page