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 types | Definition |
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 |
The C++ vector container has a number of member functions which are listed below:
Functions | Description |
vector() |
Construct a vector object. |
~vector() |
Destroys container by deallocating container memory. |
operator=() |
Assign content to a vector. |
Functions | Description |
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. |
Functions | Description |
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. |
Functions | Description |
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. |
Functions | Description |
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. |
Functions | Description |
get_allocator() |
Return a copy of allocator object associated with the vector. |
Functions | Description |
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. |