vector back() in C++

back(): It returns the read and write reference to the data at the last element of the vector.

Syntax:

vecName.back()

It is available in the below file

File: stl_vector.h

For Example: 

vec = {1,2,3,4}

vec.back() => It return 4.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intvec = {1234};

    //read the last element of the vector
    cout << vec.back() << "\n";

    //update or write to the last element of
    //the vector
    vec.back() = 10;

    cout << vec.back() << "\n";
    return 0;
}


No comments:

Post a Comment