C++ vector headers file


Some points to note about C++ vector:

*It is a dynamic container which provide fast and efficient way to access individual element in an ordered storage.

*The expansion ,contraction and the ultimate deletion of the storage is handled automatically for us.

*It has it’s own iterator that can be used to operate on the container.

Link:More information about vector here.

<vector> header file consist of two template classes:

namespace std {

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

template<class A >
class vector<bool> ;

}

*The T is the type for which the vector class is defined.
*A is a type of allocator<T> class template.

The list of all the functions and constructor available in vector header file is given below.


Constructor and destructor

vector( ) noexcept ;
explicit vector(const A& a) ;
explicit vector(size_t n) ;
explicit vector(size_t n , const A& a = A() );
vector(size_t n, const T& value ,const A& a = A());
vector(const vector& x);
vector(vector&& x) noexcept ;(added in C++11)
vector(const vector& x, const A& a) ;
vector(vector&& rv, const A& m) noexcept ; (added in C++11)
vector(initializer_list<T> l, const A& __a = A());
template< typename It > vector( It first, It last , const A& a = A() );
~vector ( );

Other member functions

 

Return
type
Function name and
argument
Function definition

Operator assignment
vector& operator=(const vector& x);
(There are two variant of this function)

Assign function
void assign(size_t n, const T& val);
(there are two variant of this function)
Fills vector with n copies of val.

iterator
iteratorbegin( ) Returns read/write iterator that points to the first element in vector.
const_iteratorcbegin() const noexceptReturns read-only iterator that points to the first element in the vector
reverse_iteratorrbegin() constReturns read-only iterator that points to last element in the vector.
const_reverse_iteratorcrbegin() const noexceptReturns a read-only reverse iterator that points to the last element in the vector
iteratorend( )Returns read/write iterator that points to one past the last element in vector.
const_iteratorcend() const noexceptReturns a read-only iterator that points one past the last element in the vector
reverse_iteratorrend( ) constReturns a read-only reverse iterator that points to one before the first element in the vector.
const_reverse_iteratorcrend() const noexceptReturns a read-only reverse iterator that points to one before the first element in the vector.

Capacity
size_tsize() constReturns number of elements stored by the vector currently.
size_tmax_size() constReturns the size of the largest possible vector.
voidresize(size_t Size)
(There is a variant of this function)
Resizes the vector to the specified number of elements.
size_tcapacity() constReturns the total number of elements that the vector can hold before needing to allocate more memory.
boolempty() constReturns true if the vector is empty.
voidreserve(size_t n)This function attempts to reserve enough memory for the vector to hold the specified number of elements.

Element access
T&operator[ ] (size_t n)Array style element accessing using the subscript.
T&at(size_t n)Access data at ‘n’ position.
T&front()Returns a read/write reference to the data at the first element of the vector.
T&back()Returns a read/write reference to the data at the last element of the vector.

Modifier
voidclear()Erases all the elements.
iteratorerase(const_iterator pos)
(There is a variant of this function)
Remove element at given position.
iteratorinsert(const_iterator pos , size_t n )
(There are many variant of this function)
Inserts a number of copies of given data into the vector.
voidpop_back( )Removes last element.
voidpush_back(const T& value )
(C++11 support moving of data)
Add data to the end of the vector.
voidswap(vector& x)Swaps data with another vector.

Functions added in C++11
voidshrink_to_fit( )A nonbinding request to reduce the size of the vector.
iteratoremplace(const_iterator pos, T&& arg)Inserts an object in vector before specified iterator.
voidemplace_back(T&& arg)Insert element at the back of the vector.
T*data( )Returns a pointer to the storage of the vector.

 

Non-member functions

template<class T, class A> booloperator==
(const vector<T, A>& lhs,
const vector<T, A>& rhs);
Vector equality comparison function.
template<class T, class A> booloperator!=
(const vector<T, A> & lhs ,
const vector<T, A> & rhs);
Not equal comparison function.
template<class T, class A>
bool
operator<
(const vector<T, A> & lhs, const vector<T, A> & rhs);
vector smaller than comparison function.
template<class T, class A>
bool
operator>
(const vector<T, A> & lhs,
const vector<T, A> & rhs);
Vector greater than comparison function
template<class T, class A>
bool
operator<=
(const vector<T, A> & lhs,
const vector<T, A> & rhs);
vector smaller than or equal to comparison function.
template<class T, class A>
bool
operator>=
(const vector<T, A> & lhs,
const vector<T, A> & rhs);
vector greater than or equal to comparison function.
template<class T, class A>
void
swap
(vector<T, A > & lhs,
vector<T, A > & rhs);
swap the elements in the two vectors.

 


Related Link

->difference between emplace_back and push_back function

->STL vector container.