
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deque at and Deque Swap in C++ Programming STL
In this article we will be discussing the working, syntax and examples of deque::at() and deque::swap() functions 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 allow user 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::at()?
deque::at() is an inbuilt function in C++ STL which is declared in <deque> header file. deque::at() returns a reference that is used to point the element present at the specified position in the deque. This function goes to the element present at the position passed as an argument to the function. The position value starts from 0.
Syntax
mydeque.at(int position);
Parameter
This function accepts one parameter for the position where we want to point.
Return value
It returns a reference to the element on the specified position of the deque container.
Example
Input: deque<int> mydeque = {10, 20, 30, 40}; mydeque.at(2);
Output
Element at 2 positions is 30.
Example
#include <deque> #include <iostream> using namespace std; int main(){ deque<int> myDeque; myDeque.push_back(90); myDeque.push_back(80); myDeque.push_back(70); myDeque.push_back(60); myDeque.push_back(50); myDeque.push_back(40); myDeque.push_back(30); myDeque.push_back(20); myDeque.push_back(10); for (int i = 0; i < myDeque.size(); ++i){ if (i % 2 == 0){ cout << myDeque.at(i); cout << " "; } } return 0; }
Output
If we run the above code it will generate the following output −
90 70 50 30 10
What is deque::swap()?
deque::swap() is an inbuilt function in C++ STL which is declared in <deque> header file. deque::swap() is used to swap the contents of one deque container with the other. This function takes an object of another deque container whose data we wish to swap with the associated deque container.
Syntax
mydeque1.swap(type_t& mydeque2);
Parameter
This function accepts one parameter i.e., the reference to the deque whose data we wish to swap with the associated deque.
Return value
It returns nothing.
Example
Input: deque<int> even = {2, 4, 6, 8}; deque<int> odd = {1, 3, 5, 7}; even.swap(odd); Output: Even deque: 1 3 5 7 Odd deque: 2 4 6 8
Example
#include <deque> #include <iostream> using namespace std; int main(){ deque<int> Deque_1 = { 1, 2, 3, 4 }; deque<int> Deque_2 = { 3, 5, 7, 9 }; Deque_1.swap(Deque_2); cout<<"Deque_1 elements after swapping : "; for (auto i = Deque_1.begin(); i< Deque_1.end(); ++i) cout << *i << " "; cout <<endl<<"Deque_2 elements after swapping : "; 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 −
Deque_1 elements after swapping : 3 5 7 9 Deque_2 elements after swapping : 1 2 3 4