C++ STL  deque constructor 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++ STL deque 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

To show: How to use the C++ STL deque constructor 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, constructor

#include <deque>

#include <iostream>

using namespace std;

 

int main(void)

{

// deque iterators

deque <int>::iterator c1_Iter, c2_Iter, c3_Iter, c4_Iter, c5_Iter, c6_Iter;

// create an empty deque c0

deque <int> c0;

// create a deque c1 with 3 elements of default value 0

deque <int> c1(3);

// create a deque c2 with 5 elements of value 2

deque <int> c2(5, 2);

// create a deque c3 with 3 elements of value 1 and with the allocator of deque c2

deque <int> c3(3, 1, c2.get_allocator());

// create a copy, deque c4, of deque c2

deque <int> c4(c2);

// create a deque c5 by copying the range c4[_First, _Last)

c4_Iter = c4.begin();

c4_Iter++;

c4_Iter++;

deque <int> c5(c4.begin(), c4_Iter);

// create a deque c6 by copying the range c4[_First, _Last) and c2 with the allocator of deque

c4_Iter = c4.begin( );

c4_Iter++;

c4_Iter++;

c4_Iter++;

 

// do the operations and print the data

deque <int> c6(c4.begin(), c4_Iter, c2.get_allocator());

cout<<"c1 deque: ";

for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)

cout<<*c1_Iter<<" ";

cout<<endl;

cout<<"c2 deque: ";

for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)

cout<<*c2_Iter<<" ";

cout<<endl;

cout<<"c3 deque: ";

for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)

cout<<*c3_Iter<<" ";

cout<<endl;

cout<<"c4 deque: ";

for (c4_Iter = c4.begin(); c4_Iter != c4.end(); c4_Iter++)

cout<<*c4_Iter<<" ";

cout<<endl;

cout<<"c5 deque: ";

for (c5_Iter = c5.begin(); c5_Iter != c5.end(); c5_Iter++)

cout<<*c5_Iter<<" ";

cout<<endl;

cout<<"c6 deque: ";

for (c6_Iter = c6.begin(); c6_Iter != c6.end(); c6_Iter++)

cout<<*c6_Iter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

c1 deque: 0 0 0

c2 deque: 2 2 2 2 2

c3 deque: 1 1 1

c4 deque: 2 2 2 2 2

c5 deque: 2 2

c6 deque: 2 2 2

Press any key to continue . . .

 

 

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