C++ Vector end
This function provides an iterator pointing to the element past the last element in the vector container.
Syntax
Consider a vector v. Syntax would be:
iterator it=v.end()
Parameter
It does not contain any parameter.
Return value
It yields an iterator that points to the element after the final one.
Example 1
Let's see a simple example.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{10,20,20,40};
vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
return 0;
}
Output:
10 20 20 40
In this instance, the elements within the vector were traversed using the begin and end methods.
Example 2
Let's see another simple example.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"Welcome","to","javaCppTutorial"};
vector<string>::iterator it;
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
return 0;
}
Output:
Welcome to javaCppTutorial
In this instance, sequences of vector elements have been traversed using the begin and end method.