English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C++ Deque (Double-ended Queue)
C ++ The Deque swap() function swaps the content of the given deque with another deque of the same type passed as an argument.
The type of the deque must be the same.
The size of the deque can be unequal.
void swap(deque& second);
second:This is another deque container whose content will be swapped with the given deque.
It does not return any value.
Let's see a simple example
#include <iostream> #include<deque> using namespace std; int main() { deque<string> str={"C is a programming language"}; deque<string> str1={"java is a programming language"}; str.swap(str1); deque<string>::iterator itr=str.begin(); deque<string>::iterator itr1=str1.begin(); cout << "After the swap, the value of str is: " <<*itr; cout << '\n'; cout << "After the swap, str1The value is: "<<"*itr1; return 0; }
Output:
After the swap, the value of str is: "java is a programming language" After the swap, str1The value is: "C is a programming language"
In this example, the swap() function swaps str and str1The content. Now, str contains "java is a programming language", and str1Containing "C is a programming language".
Let's see a simple example when two deques are of different types.
#include <iostream> #include<deque> using namespace std; int main() { deque<char> c={'m','a','n','g','o'}; deque<int> s={1 ,2,3,4,5}; c.swap(s); return 0; }
Output:
error: no matching function for call to 'std::deque<char>::swap(std::deque<int>&)'
In this example, the swap() function will cause an error because the types of the two deques are different.
Let's see a simple example when the sizes are different.
#include <iostream> #include<deque> using namespace std; int main() { deque<int> first={1,2,3,4}; deque<int> second={10,20,30,40,50}; deque<int>::iterator itr; first.swap(second); cout << "The content of the first deque:"; for(itr=first.begin();itr!=first.end();++itr) cout <<*itr << " "; cout << '\n'; cout << "Content of the second double-ended queue:"; for(itr=second.begin();itr!=second.end();++itr) cout <<*itr << " "; return 0; }
Output:
Content of the first double-ended queue:10 20 30 40 50 Content of the second double-ended queue:1 2 3 4
In this example, the swap() function exchanges the content of the first double-ended queue to the second double-ended queue.