Access Elements from a Vector in C++

In this tutorial, we will learn how to access an element from a vector in C++.

Various ways to access an element from a vector in C++

There are different ways to access an element from a vector by the index number or position. Here we will discuss some of them.

vector:: operator[ ]

The operator references the element present at the index number which is inside the brackets.

Syntax:

vector_name[index_number]

vector::at( )

This function is used to reference the element present at a position given inside the parameter. This function is similar to the operator function.

Syntax:

vector_name.at(position)

vector::front( )

The front( ) fumction is used to get the first element from the vector list.

Syntax:

vector_name.front()

vector::back( )

The back( ) function is used to get the last element from the vector list.

Syntax:

vector_name.back()

Access an element from a vector in C++

Now let us write the code to illustrate the same.

#include<iostream>
#include<vector> 
using namespace std; 
int main() 
{   vector<int>vec;
    int n,x;
    cout<<"Enter the size of the vector: ";
    cin>>n;
    cout<<"Enter vector values:\n";
    for (int i=0;i<n;i++)
    {
        cin>>x;
        vec.push_back(x);
    }

    cout<<"\nReference operator vec[3]= "<<vec[3]; 
    cout<<"\nValue at position 4 is: "<<vec.at(4); 
    cout<<"\nFirst element of the vector is: "<<vec.front(); 
    cout<<"\nLast element of the vector is: "<<vec.back(); 
    cout<<"\nAll the elements of the vector is:\n";
    
    for(int i=0;i<vec.size();i++)
    {
        cout<<vec[i]<<" ";
    }
    return 0; 
}

Output:

Enter the size of the vector: 6                                                                                                 
Enter vector values:                                                                                                            
45                                                                                                                              
87                                                                                                                              
23                                                                                                                              
6                                                                                                                               
12                                                                                                                              
77                                                                                                                              

Reference operator vec[3]= 6                                                                                                     

Value at position 4 is: 12                                                                                                      

First element of the vector is: 45                                                                                              

Last element of the vector is: 77                                                                                               

All the elements of the vector is:                                                                                              

45 87 23 6 12 77

Explanation:

In this code, first of all, we will take the input for the vector list from the user.
vec.pushback(x) pushes the elements into the vector from the last.
One by one we execute each and every function to access the elements from the vector.
The explanation for all the functions has been given above.

 

I hope this will help you to solve your problem.

You may also learn:

Insert an element into a specific position of a vector in C++

Leave a Reply

Your email address will not be published. Required fields are marked *