Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional iterator to the first element of the container.
Syntax :
vectorname.begin() Parameters : No parameters are passed. Returns : This function returns a bidirectional iterator pointing to the first element.
Examples:
Input : myvector{1, 2, 3, 4, 5};
myvector.begin();
Output : returns an iterator to the element 1
Input : myvector{"This", "is", "Geeksforgeeks"};
myvector.begin();
Output : returns an iterator to the element This
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// INTEGER VECTOR EXAMPLE // CPP program to illustrate // Implementation of begin() function #include <iostream> #include <vector> using namespace std; int main() { // declaration of vector container vector<int> myvector{ 1, 2, 3, 4, 5 }; // using begin() to print vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0; } |
Output:
1 2 3 4 5
// STRING VECTOR EXAMPLE // CPP program to illustrate // Implementation of begin() function #include <iostream> #include <string> #include <vector> using namespace std; int main() { // declaration of vector container vector<string> myvector{ "This", "is", "Geeksforgeeks" }; // using begin() to print vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0; } |
Output:
This is Geeksforgeeks
Time Complexity : O(1)
end() function is used to return an iterator pointing to next to last element of the vector container. end() function returns a bidirectional iterator.
Syntax :
vectorname.end() Parameters : No parameters are passed. Returns : This function returns a bidirectional iterator pointing to next to last element.
Examples:
Input : myvector{1, 2, 3, 4, 5};
myvector.end();
Output : returns an iterator after 5
Input : myvector{"computer", "science", "portal"};
myvector.end();
Output : returns an iterator after "portal"
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// INTEGER VECTOR EXAMPLE // CPP program to illustrate // Implementation of end() function #include <iostream> #include <vector> using namespace std; int main() { // declaration of vector container vector<int> myvector{ 1, 2, 3, 4, 5 }; // using end() to print vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0; } |
Output:
1 2 3 4 5
// STRING VECTOR EXAMPLE // CPP program to illustrate // Implementation of end() function #include <iostream> #include <string> #include <vector> using namespace std; int main() { // declaration of vector container vector<string> myvector{ "computer", "science", "portal" }; // using end() to print vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0; } |
Output:
computer science portal
Time Complexity : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- Sorting Vector of Arrays in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Minimize count of divisions by D to obtain at least K equal array elements
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
- Lexicographical smallest number after at most K consecutive swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

