What are Vectors in C++(Object Oriented Programming) and how are they used ?
![]() Don't want to miss a single bit? Subscribe By Email for Daily Jobs |
Vector is a part of the sequential container which holds an ordered collection of elements of one type. A vector holds all these elements in a contiguous area of memory. Random access of elements is efficient however insertion of elements at any other position other than the end of the vector is inefficient since each element to the right of the element to be inserted has to be shifted one by one which leads to inefficiency. Similarly deletion of an element besides the last element is inefficient as well.
The vector is a part of the std namespace and one must include #include header in order to use a vector.
#include
using namespace std;
int main()
{
vector
v1.push_back(1);
v1.push_back(2);
}
The function capacity () returns the total number of elements that the vector can hold and size () returns the number of elements that are currently stored in the vector
Related Articles
- What are Iterators in C++ and Object Oriented Programming ? Explain where we use them
- What are containers in C++ and in Object Oriented Programming ? Which objects are available as containers ?
- What are the differences between ArrayList and LinkedList types in Java ?
- Write a C program to find the depth or height of a tree.
- Write a C program to find the depth or height of a tree.


