Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 is used to return an iterator pointing to the first element of the deque container. begin() function returns a bidirectional iterator to the first element of the container.

Syntax :

dequename.begin()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the first element.

Examples:

Input  : mydeque{1, 2, 3, 4, 5};
mydeque.begin();
Output : returns an iterator to the element 1

Input : mydeque{8, 7};
mydeque.begin();
Output : returns an iterator to the element 8

Errors and Exceptions

1. It has a no exception throw guarantee.

2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of begin() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    // declaration of deque container
    deque<int> mydeque{ 1, 2, 3, 4, 5 };
 
    // using begin() to print deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


Output:

1 2 3 4 5

Time Complexity : O(1)

deque::end()

end() function is used to return an iterator pointing to the last element of the deque container. end() function returns a bidirectional iterator to the last element of the container. Note : The last element of any container is considered as the theoretical element next to the last value stored in the container.

Syntax :

dequename.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the last element.

Examples:

Input  : mydeque{1, 2, 3, 4, 5};
mydeque.end();
Output : returns an iterator to the element next to the element 5

Input : mydeque{8, 7};
mydeque.end();
Output : returns an iterator to the element next to the element 7

Errors and Exceptions

1. It has a no exception throw guarantee.

2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Implementation of end() function
#include <deque>
#include <iostream>
using namespace std;
 
int main()
{
    // declaration of deque container
    deque<int> mydeque{ 1, 2, 3, 4, 5 };
 
    // using end() to print deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


Output:

1 2 3 4 5

Time Complexity : O(1)

Let us see the differences in a tabular form -:

 

deque::begin()  deque::end
1.

It is used to return an iterator pointing to the first element in the deque container.

It is used to return an iterator referring to the past-the-end element in the deque container.

2.

Its syntax is -:

 iterator begin();

Its syntax is -:

iterator end();

3.

It does not takes any parameters.

It does not take any parameters.

4.

Its complexity is constant.

Its time complexity is constant.

5.

Its iterator validity does not changes.

Its iterator validity does not changes.



Last Updated : 27 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads