C++ Deque operator[]()

C++ Deque operator[] function is used to access the element at specified position pos. If position pos is greater than the size of container then it returns a value 0.

Difference between operator[]() & at()

When position pos is greater than the size of the container then operator[]() function returns a value 0 while at() function throws an exception i.e out of range.

Syntax

Parameter

pos: It defines the position of an element which is to be accessed.

Return value

It returns a reference to an element at position pos in the deque container.

Example 1

Let's see a simple example

Output:

mango is my favorite fruit 

In this example, operator[]() function access each element of deque a.

Example 2

Let's see a simple example when pos is out of range.

Output:

0

In this example, operator[]() function tries to access the position which is greater than the size of the container. Therefore, it returns 0.


Next TopicC++ Deque



Latest Courses