Vector the array list of C Plus Plus Programming Language

Author: Al-mamun Sarkar Date: 2020-04-03 19:15:23

Vector Array List of C Plus Plus Programming Language. The following code shows how to use vector in C++ programming language.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector < int > the_vector;

    the_vector.push_back( 1 );
    the_vector.push_back( 2 );
    the_vector.push_back( 3 );
    the_vector.push_back( 4 );

    cout << "Vector Is : " << endl;
    for (int i=0; i<the_vector.size(); i++) {
        cout << "the_vector[" << i << "] is : " << the_vector[i] << endl;
    }

    the_vector[2] = 5;
    the_vector.at( 3 ) = 10;

    cout << "New vector Is : " << endl;
    for (int i=0; i<the_vector.size(); i++) {
        cout << "the_vector[" << i << "] is : " << the_vector[i] << endl;
    }

    return 0;
}

 

Output:

Vector Is : 
the_vector[0] is : 1
the_vector[1] is : 2
the_vector[2] is : 3
the_vector[3] is : 4
New vector Is : 
the_vector[0] is : 1
the_vector[1] is : 2
the_vector[2] is : 5
the_vector[3] is : 10