deque::operator= and deque::operator[] in C++ STL
Last Updated :
14 Feb, 2023
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.
deque::operator=
This operator is used to assign new contents to the container by replacing the existing contents.
It also modifies the size according to the new contents.
Syntax :
dequename1 = (dequename2)
Parameters :
Another container of the same type.
Result :
Assign the contents of the container passed as
parameter to the container written on left side of the operator.
Examples:
Input : mydeque1 = 1, 2, 3
mydeque2 = 3, 2, 1, 4
mydeque1 = mydeque2;
Output : mydeque1 = 3, 2, 1, 4
Input : mydeque1 = 2, 6, 1, 5
mydeque2 = 3, 2
mydeque1 = mydeque2;
Output : mydeque1 = 3, 2
Errors and Exceptions
1. If the containers are of different types, an error is thrown.
2. It has a basic no exception throw guarantee otherwise.
C++
// CPP program to illustrate
// Implementation of = operator
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque1{ 1, 2, 3 };
deque<int> mydeque2{ 3, 2, 1, 4 };
mydeque1 = mydeque2;
cout << "mydeque1 = ";
for (auto it = mydeque1.begin(); it != mydeque1.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
mydeque1= 3 2 1 4
Time complexity: O(n)
Auxiliary Space: O(n)
deque::operator[]
This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at() function throws an out-of-range exception when the position is not in the bounds of the size of deque, while this operator causes undefined behavior.
Syntax :
dequename[position]
Parameters :
Position of the element to be fetched.
Returns :
Direct reference to the element at the given position.
Examples:
Input : mydeque = 1, 2, 3
mydeque[2];
Output : 3
Input : mydeque = 3, 4, 1, 7, 3
mydeque[3];
Output : 7
Errors and Exceptions
1. If the position is not present in the deque, it shows undefined behavior.
2. It has a no exception throw guarantee otherwise.
C++
// CPP program to illustrate
// Implementation of [] operator
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_back(3);
mydeque.push_back(4);
mydeque.push_back(1);
mydeque.push_back(7);
mydeque.push_back(3);
cout << mydeque[3];
return 0;
}
Output:
7
Time complexity: O(1)
Auxiliary Space: O(n)
Application
Given a deque of integers, print all the integers present at even positions.
Input :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 1, 3, 5, 7 and 9 are at position 0, 2, 4, 6 and 8 which are even
Algorithm
1. Run a loop till the size of the array.
2. Check if the position is divisible by 2, if yes, print the element at that position.
C++
// CPP program to illustrate
// Application of [] operator
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> mydeque;
mydeque.push_back(1);
mydeque.push_back(2);
mydeque.push_back(3);
mydeque.push_back(4);
mydeque.push_back(5);
mydeque.push_back(6);
mydeque.push_back(7);
mydeque.push_back(8);
mydeque.push_back(9);
// Deque becomes 1, 2, 3, 4, 5, 6, 7, 8, 9
for (int i = 0; i < mydeque.size(); ++i) {
if (i % 2 == 0) {
cout << mydeque[i];
cout << " ";
}
}
return 0;
}
Output:
1 3 5 7 9
Time complexity: O(n). // n is the size of deque.
Auxiliary Space: O(n)
Let us see the differences in a tabular form -:
| deque::operator= | deque::operator[] |
1. | It is used to assign new contents to the container and replacing its current contents | It is used to return a reference to the element at position n in the deque container. |
2. | Its syntax is -: deque& operator= (const deque& x); |
Its syntax is -:
reference operator[] (size_type n);
|
3. |
It takes two parameters that are -:
1. A deque object of the same type
2. An Initializer List object.
| It takes only one parameter that is the position of element in container. |
4. | Its complexity is linear. | Its complexity is constant. |
5. | It is defined in <deque> header file. | Its iterator validity does not changes. |
Similar Reads
Deque in C++ STL
In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.Example:C++#include <iostream> #include <deque> using
6 min read
Commonly Used Methods
deque::push_front() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
2 min read
deque::push_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
2 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque insert() function in C++ STL
The deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by in
3 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
3 min read
Deque::empty() and deque::size() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues 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 at the beginning. Unlike vectors, contiguous storage allocation
5 min read
Other Member Methods
deque max_size() function in C++ STL
The deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque c
1 min read
deque assign() function in C++ STL
The deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. Syntax: de
2 min read
deque rbegin() function in C++ STL
The deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which po
2 min read
deque rend() function in C++ STL
The deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reve
2 min read
deque cbegin() in C++ STL
The cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque cbegin() in C++ STL
The cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
How Deque Works Internally in C++?
Prerequisite: Deque in C++ Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1)
4 min read
Deque of Pairs in C++ with Examples
What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s
5 min read
Difference between Queue and Deque in C++
Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty
4 min read
Deque vs Vector in C++ STL
Deque in C++ Standard Template Library (STL) Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors but support inserting and deleting the first element in O(1). Unlike vectors, contiguous storage allocation is not guarante
2 min read
How to check/find an item in Dequeue using find() method
find() function finds the element in the given range of numbers. Returns an iterator to the first element in the range [first, last) that compares equal to the value to be searched. If no such element is found, the function returns last. Syntax: InputIterator find (InputIterator first, InputIterator
6 min read