Home Uncategorized Write a C++ Program to implement Vector and Vector Operations

Write a C++ Program to implement Vector and Vector Operations


AIM: Write a program to illustrate vector and vector operations.


Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.


using namespace std;
int main()
    cout<<“The elements are “;
    cout<<“Elements in ascending order\n”;
    for(int x=0;x<8;x++)
        int s=e.size()-1;
    int s=e.size()-1;
    cout<<“\nElements in descending order\n”;
    for(int j=s;j>=3;j–)
    return 0;


The elements are 5         8              9
Elements in ascending order
1              2              3              4              5              6              7              8
Elements in descending order
8              7              6              5              4              3              2              1

Back to Programs.