vector::front() function with example in C++ STL

C++ STL vector::front() function: Here, we are going to learn about the front() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 15, 2019

C++ vector::front() function

vector::front() is a library function of "vector" header, it is used to access the first element from the vector, it returns a reference to the first element of the vector.

Note: To use vector, include <vector> header.

Syntax of vector::front() function


Parameter(s): none – It accepts nothing.

Return value: reference – It returns a reference to the first element of vector.


    vector<int> vector1{ 1, 2, 3, 4, 5 };

    Function call:
    cout << vector1.front() << endl;


C++ program to demonstrate example of vector::front() function

//C++ STL program to demonstrate example of
//vector::front() function

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

int main()
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //accessing first element
    //using vector::front() function

    cout << "first element is: " << v1.front() << endl;

    //changing first element = 100;
    cout << "now, first element is: " << v1.front() << endl;

    return 0;


first element is: 10
now, first element is: 100

Reference: C++ vector::front()

Related Tutorials

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.