C++ STL deque constructors manipulation program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

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: none

To do: Using the C++ deque and manipulate it in C++ programming

To show: How to use the C++ deque, constructors to construct a deque of a specific size or with elements of a specific value or with a specific allocator or as a copy of all or part of some other deque in C++ programming

 

// C++ STL deque, constructors

#include <deque>

#include <iostream>

using namespace std;

 

int main(void)

{

// deque iterators

deque <int>::iterator deq0Iter, deq1Iter, deq2Iter, deq3Iter, deq4Iter, deq5Iter, deq6Iter;

// create an empty deque deq0

deque <int> deq0;

// create a deque deq1 with 10 elements of default value 0

deque <int> deq1(10);

// create a deque deq2 with 7 elements of value 10

deque <int> deq2(7, 10);

// create a deque deq3 with 4 elements of value 2 and with the allocator of deque deq2

deque <int> deq3(4, 2, deq2.get_allocator());

// create a copy, deque deq4, of deque deq2

deque <int> deq4(deq2);

 

// deque deq5 a copy of the deq4[_First, _Last) range

deq4Iter = deq4.begin();

deq4Iter++;

deq4Iter++;

deq4Iter++;

deque <int> deq5(deq4.begin(), deq4Iter);

// create a deque deq6 by copying the range deq4[_First, _Last) and the allocator of deque deq2

deq4Iter = deq4.begin();

deq4Iter++;

deq4Iter++;

deq4Iter++;

deque <int> deq6(deq4.begin(), deq4Iter, deq2.get_allocator());

// do some operations and print the results

cout<<"Operation: deque <int> deq0;"<<endl;

cout<<"deq0 deque data: ";

for(deq0Iter = deq0.begin(); deq0Iter != deq0.end(); deq0Iter++)

cout<<*deq0Iter<<" ";

cout<<endl;

cout<<"\nOperation: deque <int> deq1(10);"<<endl;

cout<<"deq1 deque data: ";

for(deq1Iter = deq1.begin(); deq1Iter != deq1.end(); deq1Iter++)

cout<<*deq1Iter<<" ";

cout<<endl;

cout<<"\nOperation: deque <int> deq2(7, 3);"<<endl;

cout<<"deq2 deque data: ";

for(deq2Iter = deq2.begin(); deq2Iter != deq2.end(); deq2Iter++)

cout<<*deq2Iter<<" ";

cout<<endl;

cout<<"\nOperation: deque <int> deq3(4, 2, deq2.get_allocator());"<<endl;

cout<<"deq3 deque data: ";

for(deq3Iter = deq3.begin(); deq3Iter != deq3.end(); deq3Iter++)

cout<<*deq3Iter<<" ";

cout<<endl;

cout<<"\nOperation: deque <int> deq4(deq2);"<<endl;

cout<<"deq4 deque data: ";

for(deq4Iter = deq4.begin(); deq4Iter != deq4.end(); deq4Iter++)

cout<<*deq4Iter<<" ";

cout<<endl;

cout<<"\nOperation1: deq4Iter++..."<<endl;

cout<<"Operation2: deque <int> deq5(deq4.begin(), deq4Iter);"<<endl;

cout<<"deq5 deque data: ";

for(deq5Iter = deq5.begin(); deq5Iter != deq5.end(); deq5Iter++)

cout << *deq5Iter<<" ";

cout << endl;

cout<<"\nOperation1: deq4Iter = deq4.begin() and deq4Iter++..."<<endl;

cout<<"Operation2: deque <int> deq6(deq4.begin(), deq4Iter, deq2.get_allocator());"<<endl;

cout<<"deq6 deque data: ";

for(deq6Iter = deq6.begin(); deq6Iter != deq6.end(); deq6Iter++)

cout<<*deq6Iter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: deque <int> deq0;

deq0 deque data:

Operation: deque <int> deq1(10);

deq1 deque data: 0 0 0 0 0 0 0 0 0 0

Operation: deque <int> deq2(7, 3);

deq2 deque data: 10 10 10 10 10 10 10

Operation: deque <int> deq3(4, 2, deq2.get_allocator());

deq3 deque data: 2 2 2 2

Operation: deque <int> deq4(deq2);

deq4 deque data: 10 10 10 10 10 10 10

Operation1: deq4Iter++...

Operation2: deque <int> deq5(deq4.begin(), deq4Iter);

deq5 deque data: 10 10 10

Operation1: deq4Iter = deq4.begin() and deq4Iter++...

Operation2: deque <int> deq6(deq4.begin(), deq4Iter, deq2.get_allocator());

deq6 deque data: 10 10 10

Press any key to continue . . .

 

 

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