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

C++ vector end() Usage and Example

C++ Vector (Container)

Returns an iterator to the end element of the current vector container.

Syntax

The syntax of the vector (vector) v is:

iterator it = v.end()

Parameters

It does not contain any parameters.

Return value

It is the iterator of the last element.

Example1

Let's look at 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 example, the elements of the vector (vector) are iterated using the begin() and end() functions.

Example2

Let's look at another simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
}
vector<string> v{"Welcome","to","w3codebox"};
vector<string>::iterator it;
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
return 0;
}

Output:

Welcome to w3codebox

In this example, the begin() and end() functions have been used to iterate over the string in the vector (vector).

C++ Vector (Container)