STL-Vector in C++ Programming


Vector is a template class in STL (Standard Template Library) of C++ programming language. The vectors are sequence containers that store elements.

  Syntax :
            vector < type parameter > vector_name ;

The program demonstrates the use of the STL vector container in C++. First, the program creates an empty vector a.

            array < int > a ;

The program then uses various member functions of the vector container to manipulate and display its contents

  • The capacity() function is used to determine the current capacity of the vector.
  • The push_back() function is used to add elements to the back of the vector.
  • The size() function is used to determine the current size of the vector.
  • The at() function is used to access an element at a specific index.
  • The front() and back() functions are used to access the first and last elements of the vector respectively.
  • The pop_back() function is used to remove the last element of the vector.
  • The clear() function is used to remove all elements from the vector.
  • The program also demonstrates the creation of a vector b with a specified size and initial value using the constructor vector(size, value) .
  • The program also demonstrates the creation of a vector c by copying the contents of vector b.
  • The begin() and end() functions are used to iterate over the elements of the vector using a loop.
  • The swap() function is used to swap the contents of vectors c and d.
#include<iostream>
#include<vector>
using namespace std;
//STL-Vector in C++
int main()
{
    vector<int> a;
    cout<<"Capacity : "<<a.capacity()<<endl;
    a.push_back(10);
    cout<<"Capacity : "<<a.capacity()<<endl;
    a.push_back(20);
    cout<<"Capacity : "<<a.capacity()<<endl;
    a.push_back(30);
    cout<<"Capacity : "<<a.capacity()<<endl;
    cout<<"Size : "<<a.size()<<endl;
    cout<<"Vector Element At 2 Index : "<<a.at(2)<<endl;
    cout<<"Vector First Element: "<<a.front()<<endl;
    cout<<"Vector Last Element: "<<a.back()<<endl;
    cout<<"Before Pop : "<<endl;
    cout<<"A : ";
    for(int x : a)
    {
        cout<<" "<<x;
    }
    cout<<endl;
    a.pop_back();
    cout<<"After Pop : "<<endl;
    cout<<"A : ";
    for(int x : a)
    {
        cout<<" "<<x;
    }
    cout<<endl;
    cout<<"Before Size : "<<a.size()<<endl;
    a.clear();
    cout<<"After Size : "<<a.size()<<endl;
    cout<<"After Capacity : "<<a.capacity()<<endl;
 
    vector<int> b(5,10);
    cout<<"B : ";
    for(int x : b)
    {
        cout<<" "<<x;
    }
    vector<int> c(b);
    cout<<endl;
    cout<<"C : ";
    for(int x : c)
    {
    cout<<" "<<x;
    }
     cout <<endl;
  for (auto i = c.begin(); i != c.end(); i++)
    cout << *i << " ";
 
     vector<int> d(5,20);
     cout<<endl;
    cout<<"C : ";
    for(int x : c)
    {
        cout<<" "<<x;
    }
    cout<<endl;
    cout<<"D : ";
    for(int x : d)
    {
        cout<<" "<<x;
    }
 
     c.swap(d);
 
    cout<<endl;
    cout<<"C : ";
    for(int x : c)
    {
        cout<<" "<<x;
    }
    cout<<endl;
    cout<<"D : ";
    for(int x : d)
    {
        cout<<" "<<x;
    }
 
    return 0;
}
 

Output

Capacity : 0
Capacity : 1
Capacity : 2
Capacity : 4
Size : 3
Vector Element At 2 Index : 30
Vector First Element: 10
Vector Last Element: 30
Before Pop :
A :  10 20 30
After Pop :
A :  10 20
Before Size : 2
After Size : 0
After Capacity : 4
B :  10 10 10 10 10
C :  10 10 10 10 10
10 10 10 10 10
C :  10 10 10 10 10
D :  20 20 20 20 20
C :  20 20 20 20 20
D :  10 10 10 10 10
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples