Home » C++ STL

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

    vector::front();

Parameter(s): none – It accepts nothing.

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

Example:

    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()






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.