English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Deque begin() Usage and Example

C++ Deque (Double-Ended Queue)

C ++ The Deque begin() function returns an iterator pointing to the first element of the deque container. If the container is empty, the iterator returned will be equal to end().

Syntax

iterator begin();

Parameters

It does not contain any parameters.

Return value

It returnsiterator pointing to the first element.

Example1

Let's look at a simple example

#include<iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> n={1,2,3;
  deque<int>::iterator itr;
  itr = n.begin();
  cout << "first element of the deque:" <<*itr;
  return 0;
}

Output:

first element of the deque:1

In this example, the begin() function returns an iterator to the first element.

Example2

Let's look at a simple example

#include<iostream>
#include<deque>
using namespace std;
int main()
{
  deque<char> ch={'C','+','+};
  deque<char>::iterator itr;
  itr=ch.begin()+2;
  cout<<*itr;
  return 0;
}

In this example, the begin() function increments2Therefore, the begin() function returns an iterator to the third element.

C++ Deque (Double-Ended Queue)