The Wayback Machine - https://web.archive.org/web/20240528061928/https://www.geeksforgeeks.org/vectorbegin-vectorend-c-stl/
Open In App

vector::begin() and vector::end() in C++ STL

Last Updated : 17 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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

CPP




// 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)

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

  1. It has a no exception throw guarantee. 
  2. Shows error when a parameter is passed.

CPP




// 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

CPP




// 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) 

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.


Previous Article
Next Article

Similar Reads

array::begin() and array::end() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array container. begin() function returns a bidirectional iterat
3 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
forward_list::begin() and forward_list::end() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, the forward list is more useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from list by the fact that the forward list keeps track of the location of only the next eleme
3 min read
list::begin() and list::end() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::begin() begin() function is used to return an iterator pointing to the first element of the l
3 min read
map::begin() and end() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::begin() begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first elemen
3 min read
set::begin() and set::end() in C++ STL
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::begin() begin() function is used to return an iterator pointing to t
4 min read
unordered_multimap begin() and end() function in C++ STL
The unordered_multimap::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the container or to the first element in one of its buckets. Syntax: unordered_multimap_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first eleme
5 min read
multimap::begin() and multimap::end() in C++ STL
multimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.begin() Parameters:
3 min read
multiset begin() and end() function in C++ STL
The multiset::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the multiset container. Since multiset always contains elements in an ordered way, begin() always points to the first element according to the sorting criterion. Syntax: iterator multiset_name.begin() Parameters: The function does not a
2 min read
match_results begin() and end() function in C++ STL
match_results::cbegin() is an inbuilt function in C++ STL that returns an iterator that points to the first match in the match_results object. Syntax: smatch_name.begin()Parameters: This function does not accept any parameters. Return value: This function returns an iterator pointing to the first match in the match_results object. The matches conta
3 min read
Article Tags :
Practice Tags :