
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
vector::begin() and vector::end() in C++ STL
In C++, a vector is a dynamic array provided by the Standard Template Library (STL) that can grow or shrink in size and can store multiple elements of the same type (like int, string, etc.).
Instead of accessing elements one by one using indexes like vec[0], vec[1], etc., The C++ provides helper functions like begin() and end() that return iterators. These iterators make it easy to loop through the vector from start to end, especially for loops.
The vector::begin() Function
The vector::begin() function returns an iterator pointing to the first element of the vector.
Syntax
vectorname.begin()
Parameters
This function does not take any parameters.
Return Value
This function returns an iterator to the first element in the vector.
Example
In this example, we use the begin() function to retrieve the first element of the vector {100, 200, 300, 400}:
#include<iostream> #include<vector> using namespace std; int main() { vector<int> nums = {100, 200, 300, 400}; // begin() returns an iterator to the first element auto it = nums.begin(); cout<<"The first element of the vector is: "<<*it<<endl; return 0; }
The first element of the vector is: 100
The vector::end() Function
The vector::end() function returns an iterator pointing just after the last element of the vector. To access the actual last element, use end() - 1.
Syntax
vectorname.end()
Parameters
This function does not accepts any parameter.
Return Value
This function returns an iterator to one position past the last element in the vector.
Example
In this example, we use the end() function to retrieve the last element of the vector {"Revu", "Indu", "Nagu", "Prasu"}:
#include<iostream> #include<vector> using namespace std; int main() { vector<string> names = {"Revu", "Indu", "Nagu", "Prasu"}; // end() points one position past the last element // To get the last element, use end() - 1 auto it = names.end() - 1; cout<<"The last element of the vector is: "<<*it<<endl; return 0; }
The last element of the vector is: Prasu
Both begin() and end() has no exception throw guarantee but shows an error when parameter is passed.
Using Both begin() and end() Functions
Using both begin() and end() function together we can retrieve a range of element starting from begin() up to end().
Example
In this example, we print all the elements of an integer vector using an iterator of both begin() and end() in a loop:
#include<iostream> #include<vector> using namespace std; int main() { vector<int> numbers = {10, 20, 30, 40, 50}; cout<<"Elements of the vector: "; for (auto it = numbers.begin(); it != numbers.end(); ++it) { cout<<*it<<" "; } return 0; }
Elements of the vector: 10 20 30 40 50