Open In App

vector::front() and vector::back() in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

vector::front()

This function can be used to fetch the first element of a vector container.
Syntax : 

vectorname.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the vector container.

Examples: 

Input  :  myvector = 1, 2, 3
          myvector.front();
Output :  1

Input  :  myvector = 3, 4, 1, 7, 3
          myvector.front();
Output :  3

Errors and Exceptions
1. If the vector container is empty, it causes undefined behavior. 
2. It has a no exception throw guarantee if the vector is not empty.

Time Complexity – Constant O(1)

CPP




// CPP program to illustrate
// Implementation of front() function
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<int> myvector;
    myvector.push_back(3);
    myvector.push_back(4);
    myvector.push_back(1);
    myvector.push_back(7);
    myvector.push_back(3);
    // Vector becomes 3, 4, 1, 7, 3
 
    cout << myvector.front();
    return 0;
}


Output: 

3
vector::back()

This function can be used to fetch the last element of a vector container.
Syntax : 

vectorname.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the vector container.

Examples: 

Input  :  myvector = 1, 2, 3
          myvector.back();
Output :  3

Input  :  myvector = 3, 4, 1, 7, 2
          myvector.back();
Output :  2

Errors and Exceptions
1. If the vector container is empty, it causes undefined behavior. 
2. It has a no exception throw guarantee if the vector is not empty.

Time Complexity – Constant O(1)
 

CPP




// CPP program to illustrate
// Implementation of back() function
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<int> myvector;
    myvector.push_back(3);
    myvector.push_back(4);
    myvector.push_back(1);
    myvector.push_back(7);
    myvector.push_back(2);
    // Vector becomes 3, 4, 1, 7, 2
 
    cout << myvector.back();
    return 0;
}


Output: 

2

Let us see the differences in a tabular form -:

  vector::front() vector::back() 
1. It is used to return a reference to the first element in the vector. It is used to return a reference to the last element in the vector.
2.

Its syntax is -:

vectorName.front();

Its syntax is -:

vectorName.back();

3. It works on front (left) side of the vector. It works on rear(right) side of the vector.
4. It does not take any parameters. It does not take any parameters.
5. Its complexity is constant. Its complexity is constant.
6. Its iterator validity does not changes. Its iterator validity does not changes.


Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads