Vectors are the 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.
vector::begin()
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.
Return Type:
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
- It has a no exception throw guarantee.
- Shows error when a parameter is passed.
CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > myvector{ 1, 2, 3, 4, 5 };
for ( auto it = myvector.begin();
it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
1 2 3 4 5
CPP
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> myvector{ "This" , "is" ,
"Geeksforgeeks" };
for ( auto it = myvector.begin();
it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
This is Geeksforgeeks
Time Complexity: O(1)
vector::end()
end() function is used to return an iterator pointing next to last element of the vector container. end() function returns a bidirectional iterator.
Syntax :
vectorname.end()
Parameters :
No parameters are passed.
Return Type:
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
- It has a no exception throw guarantee.
- Shows error when a parameter is passed.
CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > myvector{ 1, 2, 3, 4, 5 };
for ( auto it = myvector.begin();
it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
|
Output:
1 2 3 4 5
CPP
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> myvector{ "computer" , "science" ,
"portal" };
for ( auto it = myvector.begin(); it != myvector.end();
++it)
cout << ' ' << *it;
return 0;
}
|
Output:
computer science portal
Time Complexity: O(1)
Let us see the differences in a tabular form is as follows:
vector::begin() |
vector::end() |
It is used to return an iterator pointing to the first element in the vector. |
It is used to return an iterator referring to the past-the-end element in the vector container. |
Its syntax is -:
iterator begin();
|
Its syntax is -:
iterator end();
|
It does not take any parameters. |
It does not take any parameters. |
Its complexity is constant. |
Its complexity is constant. |
Its iterator validity does not change. |
Its iterator validity does not change. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Jun, 2022
Like Article
Save Article