English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and example of vector back()

C++ Vector (Container)

The function returns the currentvectorReference to the last element.

Syntax

The syntax of vector v is:

v.back();

Parameter

This function does not contain any parameters.

Return value

This function returns the last element of the vector

Online example

Let's look at a simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<string> fruit{"mango","apple","banana"};
    cout << fruit.back();
    return 0; 
}

Output:

banana

In this example, the back() function displays the last element of the vector named 'fruit'.

C++ Vector (Container)