# deque::operator= and deque::operator[] in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of deque::operator= and deque::operator[] in C++ STL.

## What is Deque?

Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allows users to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the insertion and deletion of data is possible at both the ends.

## What is deque::operator =?

deque::operator = is used to assign the new values to the deque container by replacing the already existing ones. This operator also modifies the size of the deque container according to the new values.

## Syntax

mydeque1 = mydeque2;

It needs another deque container of the same type.

## Return value

The operator returns *this pointer of the deque container whose data we want to assign.

## Example

Input: deque<int> odd = {1, 3, 5, 7};
Deque<int> eve = {2, 4, 6};
odd = eve;
Output:
Odd: 2, 4, 6
Eve: 2, 4, 6

## Example

Live Demo

#include <deque>
#include <iostream>
using namespace std;
int main(){
deque<int> Deque_1 = { 10, 20, 30 };
deque<int> Deque_2 = { 30, 20, 10 };
deque<int> Deque_3 = {};
//it will swap the elements of both the deque
Deque_3 = Deque_2;
Deque_2 = Deque_1;
Deque_1 = Deque_3;
cout<<"Elements in Deque_1 are: ";
for (auto i = Deque_1.begin(); i!= Deque_1.end(); ++i)
cout << ' ' << *i;
cout<<"\nElements in Deque_2 are: ";
for (auto i = Deque_2.begin(); i!= Deque_2.end(); ++i)
cout << ' ' << *i;
return 0;
}

## Output

If we run the above code it will generate the following output −

Elements in Deque_1 are: 30, 20, 10
Elements in Deque_2 are: 10 20 30

## What is deque::operator []?

deque::operator [] is used to access the element on a specified location. This operator returns a reference to the element present at the given location in the []. This works in a similar way as the deque::at(). When we request an element position which is out of the container then the operator throws out_of_range exception. Initially the position of the deque container starts with 0.

## Syntax

mydeque[postion];

It requires a position which we want to fetch.

## Return value

This operator returns a direct reference of the element which is present at the specified position.

## Example

Input: deque<int> mydeque = {1, 2, 3, 4, 5, 6};
mydeque[2];
Output:
3

## Example

Live Demo

#include <deque>
#include <iostream>
using namespace std;
int main(){
deque<int> Deque = {10, 20, 30, 40, 50};
cout<<"Elements are : ";
for (int i = 0; i < Deque.size(); ++i){
if (i % 2 != 0){
cout << Deque[i];
cout << " ";
}
}
return 0;
}

## Output

If we run the above code it will generate the following output −

Elements are : 20 40
Updated on 05-Mar-2020 11:07:36

Advertisements