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


In this article we will be discussing the working, syntax and examples of deque::begin() and deque::end() functions in C++ STL.

What is Deque?

Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the insertion and deletion of data is possible at both the ends.

What is deque::begin()?

deque::begin() is an inbuilt function in C++ STL which is declared in header file. deque::begin() returns an iterator which is referencing to the first element of the deque container associated with the function. Both begin() and end() are used to iterate through the deque container.

Syntax

mydeque.begin();

Parameter

This function accepts no parameter

Return value

It returns an iterator pointing to the first element in the deque container.

Example

Input: deque<int> mydeque = {10, 20, 30, 40};
   mydeque.begin();
Output:
   Element at the beginning is =10

Example

 Live Demo

#include <deque>
#include <iostream>
using namespace std;
int main(){
   deque<int> Deque = {2, 4, 6, 8, 10 };
   cout<<"Elements are : ";
   for (auto i = Deque.begin(); i!= Deque.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements are : 2 4 6 8 10

What is deque::end()?

deque::end() is an inbuilt function in C++ STL which is declared in<deque> header file. deque::end() returns an iterator which is referencing next to the last element of the deque container associated with the function. Both begin() and end() are used to iterate through the deque container.

Syntax

mydeque.end();

Parameter

This function accepts no parameter

Return value

It returns an iterator pointing next to the last element in the deque container.

Example

Input: deque<int> mydeque = {10, 20, 30, 40};
   mydeque.end();
Output:
   Element at the ending is =5 //Random value which is next to the last element.

Example

 Live Demo

#include <deque>
#include <iostream>
using namespace std;
int main(){
   deque<int> Deque = { 10, 20, 30, 40};
   cout<<"Elements are : ";
   for (auto i = Deque.begin(); i!= Deque.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements are : 10 20 30 40

Updated on: 05-Mar-2020

924 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements