end(): It returns a read/write iterator that points to one past the last element in the vector.
Note: Iteration is done in ordinary element order.
File: stl_vector.h
Parameter: No parameter is required for this function.
Syntax:
vecName.end()
For Example:
vec = {1,2,3,4,5}
vec.end() = > It points beyond 5.
So, to print last element we can subtract 1 from the iterator and then print.
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){vector<int> vec = {1, 2, 3, 4, 5};vector<int>::iterator it = vec.end();cout << *(it - 1) << "\n";return 0;}
No comments:
Post a Comment