×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

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

Syntax of vector::front() function

vector::front();

Parameter(s)

none – It accepts nothing.

Return value

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

Sample Input and Output

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

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

Output:
1

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
    v1.at(0) = 100;
    cout << "now, first element is: " << v1.front() << endl;

    return 0;
}

Output

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

Reference: C++ vector::front()

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.