deque::begin
deque::begin is used to return an iterator pointing to the first element in the deque container. It returns a random access iterator pointing to it.
Syntax:
iterator begin();
Parameters: None
Return Value: Its return value is an iterator to the beginning of the sequence container.
Iterator Validity: No change in Iterator Validity
Header File:
<deque>
Exceptions: deque::begin never throws an exception
Example:
C++
// C++ program to implement deque::begin #include <deque> #include <iostream> using namespace std; // Driver code int main() { // Declaration of Deque deque< int > GFG = { 1, 2, 3, 4, 5 }; // Iterator Pointing to the first element in the // container deque< int >::iterator itr = GFG.begin(); cout << "Elements in Deque are : " ; for (itr = GFG.begin(); itr != GFG.end(); ++itr) { cout << *itr << " " ; } cout << endl; return 0; } |
Output:
Elements in Deque are : 1 2 3 4 5
- Time Complexity: O(N)
- Space Complexity: O(1)
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference between deque::assign and deque::at in C++.