C++ Standard Library C++ STL Library

C++ <deque> - begin() Function



The C++ deque::begin function returns the iterator pointing to the first element of the deque. Please note that, Unlike the deque::front function, which returns a direct reference to the first element, it returns the iterator pointing to the same element of the deque.

C++ begin end

Syntax

iterator begin();
const_iterator begin() const;
iterator begin() noexcept;
const_iterator begin() const noexcept;

Parameters

No parameter is required.

Return Value

An iterator to the beginning of the sequence container. If the sequence object is constant qualified, the function returns a const_iterator, else returns an iterator.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the deque::begin function returns the iterator pointing to the first element of the deque called MyDeque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<string> MyDeque{"Alpha","Coding","Skills"};
  deque<string>::iterator it;

  it = MyDeque.begin();
  cout<<*it<<" ";
  it++;
  cout<<*it<<" ";
  it++;
  cout<<*it<<" ";
  return 0;
}

The output of the above code will be:

Alpha Coding Skills

Example:

Lets see another example where the deque called MyDeque contains integer values and deque::begin function is used with deque::end function to specify a range including all elements of the deque container.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque{10, 20, 30, 40, 50};
  deque<int>::iterator it;

  for(it = MyDeque.begin(); it != MyDeque.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

10 20 30 40 50 

❮ C++ <deque> Library

5