What are Vectors in C++(Object Oriented Programming) and how are they used ?

October 28, 2007 · Filed Under C++ Questions, Placement Questions 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

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;
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


Get Latest JobsAdda.com Jobs, news & updates  via Email