C++ Deque cbegin
The cbegin function in C++ for Deque provides a constant iterator that points to the initial element within the deque container. This iterator retains the flexibility to be both incremented and decremented similar to the iterator obtained from the begin function. In cases where the container is devoid of elements, the iterator returned will be equivalent to the cend.
Syntax
const_iterator cbegin();
Parameter
It does not contain any parameter.
Return value
It yields a fixed iterator indicating the start of the collection.
Example 1
Let's see a simple example
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<string> fruit={"mango","apple","banana","kiwi"};
deque<string>::const_iterator itr;
itr=fruit.cbegin();
cout<<*itr;
return 0;
}
Output:
In this instance, the cbegin method provides a constant iterator pointing to the start of the container.
Example 2
Let's see a simple example
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> k={100,200,300,400,500};
deque<int>::const_iterator itr;
itr=k.cbegin()+3;
cout<<*itr;
return 0;
}
Output:
In this instance, the cbegin method is increased by 3, resulting in an iterator pointing to the 4th element.