C++ STL  deque, swap() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Using the C++ swap() for deque to exchange the elements of two deques in C++ programming

To show: How to use the C++ deque, swap to exchange the elements of two deques in C++ programming

 

// C++ STL deque, swap()

#include <deque>

#include <iostream>

using namespace std;

 

int main(void)

{

// containers

deque <int> con1, con2, con3;

// iterators

deque <int>::iterator con1_Iter, con2_Iter, con3_Iter;

 

// push/insert data

con1.push_back(1);

con1.push_back(2);

con1.push_back(3);

con2.push_back(10);

con2.push_back(20);

con3.push_back(100);

 

// print the data and do other operations

cout<<"The original deque con1 is:";

for (con1_Iter = con1.begin(); con1_Iter != con1.end(); con1_Iter++)

cout<<" "<<*con1_Iter;

cout<<endl;

cout<<"The deque con2 is:";

for (con2_Iter = con2.begin(); con2_Iter != con2.end(); con2_Iter++)

cout<<" "<<*con2_Iter;

cout<<endl;

cout<<"The deque con3 is:";

for (con3_Iter = con3.begin(); con3_Iter != con3.end(); con3_Iter++)

cout<<" "<<*con3_Iter;

cout<<endl;

con1.swap(con2);

cout<<"\nAfter swapping with con2, deque con1 is:";

for (con1_Iter = con1.begin(); con1_Iter != con1.end(); con1_Iter++)

cout<<" "<<*con1_Iter;

cout<<endl;

swap(con1,con3);

cout<<"After swapping with con3, deque con1 is:";

for (con1_Iter = con1.begin(); con1_Iter != con1.end(); con1_Iter++)

cout<<" "<<*con1_Iter;

cout<<endl;

swap<>(con1, con2);

cout<<"After swapping with con2, deque con1 is:";

for (con1_Iter = con1.begin(); con1_Iter != con1.end(); con1_Iter++)

cout<<" "<<*con1_Iter;

cout<<endl;

return 0;

}

 

Output examples:

 

The original deque con1 is: 1 2 3

The deque con2 is: 10 20

The deque con3 is: 100

After swapping with con2, deque con1 is: 10 20

After swapping with con3, deque con1 is: 100

After swapping with con2, deque con1 is: 1 2 3

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index