
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- deque::at() and deque::swap() in C++ STL
- deque::begin() and deque::end in C++ STL
- deque::empty() and deque::size() in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- deque front( ) and deque back( ) in C++ in STL
- Deque emplace_front( ) and deque emplace_back( ) in C++ in STL
- deque::push_front() in C++ STL
- Deque shrink_to_fit in C++ STL
- deque push_back( ) in C++ in STL
- deque assign() function in C++ STL
- C++ Program to Implement Deque in STL
- DEQUE CRBEGIN() in C++
- DEQUE CBEGIN() in C++
- A Deque Class in C#
- Deque in Java
