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 not be guaranteed.
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.
// 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
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 behaviour.
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 behaviour.
2. It has a no exception throw guarantee otherwise.
// 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
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.
// 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
Recommended Posts:
- Synchronous Chatting Application using C++ boost::asio
- C/C++ For loop with Examples
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
- How to implement our own Vector Class in C++?
- Web Programming in C++
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



