C++ Standard Library C++ STL Library

C++ - <vector>



C++ <vector>

Vector is a sequence container implementing array that can change in size. Unlike array, the size of a vector changes automatically when elements are appended or deleted.

Vector stores elements in contiguous memory location which enables direct access of any elements using operator []. To support shrink and expand functionality at runtime, vector container may allocate some extra storage. Hence, compared to array, vector requires more memory and in return it manage memory dynamically and efficiently.

Syntax

template < class T, class Alloc = allocator<T> > class vector; 

Parameters

T Type of the elements stored in the container.
Alloc Type of the allocator object used to define the storage allocation model, default: allocator.

Member Types

Member typesDefinition
value_type T (First template parameter)
allocator_type Alloc (Second template parameter), default: allocator<value_type>
reference value_type&
const_reference const value_type&
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a random access iterator to value_type
const_iterator a random access iterator to const value_type
reverse_iterator reverse_iterator <iterator>
const_reverse_iterator reverse_iterator <const_iterator>
difference_type ptrdiff_t
size_type size_t

C++ <vector> - Member Functions

The C++ vector container has a number of member functions which are listed below:

FunctionsDescription
vector() Construct a vector object.
~vector() Destroys container by deallocating container memory.
operator=() Assign content to a vector.

Capacity

FunctionsDescription
empty() Checks whether the vector is empty or not.
size() Returns the length of the vector in terms of bytes.
resize() Changes the size of the vector by specified number of elements.
max_size() Returns the maximum length of the vector.
capacity() Returns size of allocated space to the vector.
reserve() Requests to reserve the vector capacity be at least enough to contain n elements.
shrink_to_fit() Reduces the capacity of the vector equal to fit its size.

Element Access

FunctionsDescription
at() Access an element of the vector.
operator[]() Access an element of the vector.
front() Access first element of the vector.
back() Access last element of the vector.
data() Returns a pointer to the first element of the vector.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the vector.
end() Returns iterator pointing to the past-the-last element of the vector.
rbegin() Returns reverse iterator to the last element of the vector.
rend() Returns reverse iterator to the element preceding the first element of the vector.
cbegin() Returns const_iterator pointing to the first element of the vector.
cend() Returns const_iterator pointing to the past-the-last element of the vector.
crbegin() Returns const_reverse_iterator to the last element of the vector.
crend() Returns const_reverse_iterator to the element preceding the first element of the vector.

Modifiers

FunctionsDescription
assign() Assign vector content.
clear() Clears all elements of the vector.
pop_back() Deletes last element of the vector.
push_back() Adds a new element at the end of the vector.
erase() Deletes either a single element or range of elements from a vector.
insert() Insert elements in the vector.
swap() Exchanges elements between two vectors.
emplace() Constructs and inserts a new element at specified position in the vector
emplace_back() Constructs and inserts a new element at the end of the vector.

Allocator

FunctionsDescription
get_allocator() Return a copy of allocator object associated with the vector.

C++ <vector> - Non-member Functions

FunctionsDescription
operator == Checks whether two vectors are equal or not.
operator != Checks whether two vectors are unequal or not.
operator < Checks whether the first vector is less than the other or not.
operator > Checks whether the first vector is greater than the other or not.
operator <= Checks whether the first vector is less than or equal to the other or not.
operator >= Checks whether the first vector is greater than or equal to the other or not.
swap() Exchanges elements between two vectors.