×

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::end() function with example in C++ STL

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

C++ vector::end() function

vector::end() is a library function of "vector" header, it can be used to get the last element of a vector. It returns an iterator pointing to the past-the-end element of the vector.

Note:

  • To use vector, include <vector> header.
  • It does not point to the last element, thus to get the last element we can use vector::end()-1.

Syntax

Syntax of vector::end() function

    vector::end();

Parameter(s)

none – It accepts nothing.

Return value

iterator – It returns an iterator pointing to the past-the-end element of the vector.

Sample Input and Output

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

Function call:
vector<int>::iterator it;
it = vector1.end()-1;
cout << *it << endl;

Output:
5

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

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

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

int main()
{
    vector<int> v1;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    //creating iterator
    vector<int>::iterator it;
    it = v1.end()-1;
    cout << "last element is: " << *it << endl;

    return 0;
}

Output

last element is: 50

Reference: C++ vector::end()

Related Tutorials

Comments and Discussions!

Load comments ↻





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