# deque::at() and deque::swap() in C++ STL

Last Updated : 27 Jun, 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::at()

at() function is used reference the element present at the position given as the parameter to the function.

Syntax :

`dequename.at(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.at(2);Output :  3Input  :  mydeque = 3, 4, 1, 7, 3          mydeque.at(3);Output :  7`

Errors and Exceptions

1. If the position is not present in the deque, it throws out_of_range.

2. It has a strong no exception throw guarantee otherwise.

## CPP

 `// CPP program to illustrate` `// Implementation of at() function` `#include ` `#include ` `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.at(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, 9Output :1 3 5 7 9Explanation - 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

 `// CPP program to illustrate` `// Application of at() function` `#include ` `#include ` `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.at(i);` `            ``cout << " ";` `        ``}` `    ``}` `    ``return` `0;` `}`

Output:

`1 3 5 7 9`
deque::swap()

This function is used to swap the contents of one deque with another deque of same type and size.

Syntax :

`dequename1.swap(dequename2)Parameters :The name of the deque with whichthe contents have to be swapped.Result :All the elements of the 2 deque are swapped.`

Examples:

`Input  : mydeque1 = {1, 2, 3, 4}         mydeque2 = {3, 5, 7, 9}         mydeque1.swap(mydeque2);Output : mydeque1 = {3, 5, 7, 9}         mydeque2 = {1, 2, 3, 4}Input  : mydeque1 = {1, 3, 5, 7}         mydeque2 = {2, 4, 6, 8}         mydeque1.swap(mydeque2);Output : mydeque1 = {2, 4, 6, 8}         mydeque2 = {1, 3, 5, 7}`

Errors and Exceptions

1. It throws an error if the deque are not of the same type.

2. It throws error if the deque are not of the same size.

3. It has a basic no exception throw guarantee otherwise.

## CPP

 `// CPP program to illustrate` `// Implementation of swap() function` `#include ` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``// deque container declaration` `    ``deque<``int``> mydeque1{ 1, 2, 3, 4 };` `    ``deque<``int``> mydeque2{ 3, 5, 7, 9 };`   `    ``// using swap() function to swap elements of deques` `    ``mydeque1.swap(mydeque2);`   `    ``// printing the first deque` `    ``cout << "mydeque1 = ";` `    ``for` `(``auto` `it = mydeque1.begin(); it < mydeque1.end(); ++it)` `        ``cout << *it << " ";`   `    ``// printing the second deque` `    ``cout << endl` `         ``<< "mydeque2 = ";` `    ``for` `(``auto` `it = mydeque2.begin(); it < mydeque2.end(); ++it)` `        ``cout << *it << " ";` `    ``return` `0;` `}`

Output:

`mydeque1 = 3 5 7 9 mydeque2 = 1 2 3 4 `

Time complexity: O(n). // n is the number of elements in the deque.
Auxiliary space: O(n).

Let us see the differences in a tabular form -:

 deque::at() deque::swap() 1. It is used to return a reference to the element at position n in the deque container object. It is used to exchange the content of the container by the content of another container which is containing elements of the same type. 2. Its syntax is -: reference at (size_type n); Its syntax is -: swap (deque& x); 3. It only take one parameter that is the position of an element in the container. It only take one parameter that is the queue with which we want to exchange the elements. 4. Its complexity is constant. It does not have any return value. 5. Its iterator validity does not changes. Its complexity is constant.

Previous
Next