constructors
#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str[]={"Alex","John","Robert"};
// empty deque object
deque<int> d1;
// creates deque with 10 empty elements
deque<int> d2(10);
// creates deque with 10 elements,
// and assign value 0 for each
deque<int> d3(10,0);
// creates deque and assigns
// values from string array
deque<string> d4(str+0,str+3);
deque<string>::iterator sIt = d4.begin();
while ( sIt != d4.end() )
cout << *sIt++ << " ";
cout << endl;
// copy constructor
deque<string> d5(d4);
for ( int i=0; i<3; i++ )
cout << d5[i] << " ";
cout << endl;
return 0;
}
OUTPUT:
// Alex John Robert
// Alex John Robert
assign
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
int ary[]={1,2,3,4,5};
deque<int> d;
// assign to the "d" the contains of "ary"
d.assign(ary,ary+5);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// replace d for 3 copies of 100
d.assign(3,100);
copy(d.begin(),d.end(),
ostream_iterator(cout," "));
cout << endl;
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 100 100 100
at
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
deque<int> d(3,0);
d[0] = 100;
d.at(1) = 200;
for ( int i=0; i<3; i++ )
cout << d.at(i) << " ";
cout << endl;
return 0;
}
OUTPUT:
// 100 200 0
back
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
template<class T, class D>
class Member
{
public:
Member(T t, D d) : name(t), sal(d) {}
void print();
private:
T name;
D sal;
};
template<class T, class D>
void Member::print()
{
cout << name << " " << sal << endl;
}
//======================================
int main ()
{
typedef Member<string,double> M;
deque<M> d;
d.push_back(M("Robert",60000));
d.push_back(M("Linda",75000));
deque<M>::iterator It = d.begin();
cout << "Entire deque:" << endl;
while ( It != d.end() )
(It++)->print();
cout << endl;
cout << "Return from back()" << endl;
d.back().print();
return 0;
}
OUTPUT:
// Entire deque:
// Robert 60000
// Linda 75000
//
// Return from back()
// Linda 75000
begin
#include <iostream>
#include <deque>
#include <iterator>
#include <numeric>
using namespace std;
int main ()
{
deque<int> d(5);
iota(d.begin(),d.end(),1);
deque<int>::iterator It = d.begin();
while ( It != d.end() )
cout << *It++ << " ";
cout << endl;
// third element of the deque
It = d.begin()+2;
cout << *It << endl;
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 3
clear
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//==============================
int main ()
{
deque<int> d(10);
Print<int> print;
fill(d.begin(),d.end(),5);
cout << "Deque d : ";
for_each(d.begin(),d.end(),print);
cout << endl;
cout << "Size of d = " << d.size()
<< endl;
cout << "d.clear" << endl;
d.clear();
cout << "Deque d : ";
for_each(d.begin(),d.end(),print);
cout << endl;
cout << "Size of d = " << d.size()
<< endl;
cout << "Deque d is ";
d.empty() ? cout << "" : cout << "not ";
cout << "empty" << endl;
return 0;
}
// Deque d : 5 5 5 5 5 5 5 5 5 5
// Size of d = 10
// d.clear
// Deque d :
// Size of d = 0
// Deque d is empty
empty
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
deque<int> d;
cout << "Deque is ";
d.empty() ? cout << "" : cout << "not ";
cout << "empty" << endl;
d.push_back(100);
cout << "Deque is ";
d.empty() ? cout << "" : cout << "not ";
cout << "empty" << endl;
return 0;
}
// Deque is empty
// Deque is not empty
end
#include <iostream>
#include <deque>
#include <iterator>
#include <numeric>
using namespace std;
int main ()
{
deque<int> d(5);
iota(d.begin(),d.end(),1);
deque<int>::iterator It = d.begin();
while ( It != d.end() )
cout << *It++ << " ";
cout << endl;
// last element of the deque
It = d.end()-1;
cout << *It << endl;
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 5
erase
#include <iostream>
#include <deque>
#include <iterator>
#include <algorithm>
using namespace std;
int main ()
{
deque<int> d(10);
deque<int>::iterator It;
for ( int i=0; i<10; i++ )
d[i] = i+1;
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
It = d.begin()+2;
// remove third element
d.erase(It);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
It = d.begin();
// remove 2 elements from beginning fo d
d.erase(It,It+2);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
OUTPUT:
// 1 2 3 4 5 6 7 8 9 10
// 1 2 4 5 6 7 8 9 10
// 4 5 6 7 8 9 10
front
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
template<class T, class D>
class Member
{
public:
Member(T t, D d) : name(t), sal(d) {}
void print();
private:
T name;
D sal;
};
template<class T, class D>
void Member::print()
{
cout << name << " " << sal << endl;
}
//======================================
int main ()
{
typedef Member<string,double> M;
deque<M> d;
d.push_back(M("Linda",75000));
d.push_back(M("Robert",60000));
deque<M>::iterator It = d.begin();
cout << "Entire deque:" << endl;
while ( It != d.end() )
(It++)->print();
cout << endl;
cout << "Return from front()" << endl;
d.front().print();
return 0;
}
OUTPUT:
// Entire deque:
// Linda 75000
// Robert 60000
//
// Return from front()
// Linda 75000
insert
#include <iostream>
#include <deque>
#include <iterator>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
int ary[5];
fill(ary,ary+5,1);
deque<int> d;
deque<int>::iterator It;
Print<int> print;
copy(ary,ary+5,
back_inserter(d));
cout << "deque d : ";
for_each(d.begin(),d.end(),print);
cout << endl;
It = d.begin();
// insert value "5" at the position "It"
cout << "d.insert(It,5) : ";
d.insert(It,5);
for_each(d.begin(),d.end(),print);
cout << endl;
// insert range ary+2 - ary+5 at the position "It"
It = d.begin()+5;
cout << "d.insert(It,ary+2,ary+5 : ";
d.insert(It,ary+2,ary+5);
for_each(d.begin(),d.end(),print);
cout << endl;
// insert 2 value of "20" at the position "It"
It = d.end()-2;
cout << "d.insert(It,2,20) : ";
d.insert(It,2,20);
for_each(d.begin(),d.end(),print);
cout << endl;
return 0;
}
OUTPUT:
// deque d : 1 1 1 1 1
// d.insert(It,5) : 5 1 1 1 1 1
// d.insert(It,ary+2,ary+5 : 5 1 1 1 1 1 1 1 1
// d.insert(It,2,20) : 5 1 1 1 1 1 1 20 20 1 1
max_size
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
deque<int> d(10);
cout << "Size of d = "
<< d.size() << endl;
cout << "Max_size of d = "
<< d.max_size() << endl;
return 0;
}
OUTPUT:
// Size of d = 10
// Max_size of d = 1073741823
pop_back
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
deque<int> d;
Print<int> print;
for ( int i=0; i<5; i++ )
d.push_back(i+1);
while ( !d.empty() )
{
for_each(d.begin(),d.end(),print);
cout << endl;
d.pop_back();
}
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 1 2 3 4
// 1 2 3
// 1 2
// 1
pop_front
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
deque<int> d;
Print<int> print;
for ( int i=0; i<5; i++ )
d.push_back(i+1);
while ( !d.empty() )
{
for_each(d.begin(),d.end(),print);
cout << endl;
d.pop_front();
}
return 0;
}
OUTPUT:
// 1 2 3 4 5
// 2 3 4 5
// 3 4 5
// 4 5
// 5
push_back
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
template <class T>
class Name
{
public:
Name(T t) : name(t) {}
void print()
{
cout << name << " ";
}
private:
T name;
};
//=============================
int main ()
{
typedef Name<string> N;
typedef deque<N> D;
D d;
N n1("Robert");
N n2("Alex");
d.push_back(n1);
d.push_back(n2);
// unnamed object of the type Name
d.push_back(N("Linda"));
D::iterator It = d.begin();
while ( It != d.end() )
(It++)->print();
cout << endl;
return 0;
}
OUTPUT:
// Robert Alex Linda
push_front
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
template <class T>
class Name
{
public:
Name(T t) : name(t) {}
void print()
{
cout << name << " ";
}
private:
T name;
};
//=============================
int main ()
{
typedef Name<string> N;
typedef deque<N> D;
D d;
N n1("Robert");
N n2("Alex");
d.push_front(n1);
d.push_front(n2);
// unnamed object of the type Name
d.push_front(N("Linda"));
D::iterator It = d.begin();
while ( It != d.end() )
(It++)->print();
cout << endl;
return 0;
}
OUTPUT:
// Linda Alex Robert
rbegin and rend
#include <iostream>
#include <iomanip>
#include <deque>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
class ID
{
friend bool operator < ( const ID&, const ID& );
public:
ID(string name,int score) : name(name), score(score) {}
void display ()
{
cout.setf(ios::left);
cout << setw(3) << score << name << endl;
}
private:
string name; int score;
};
//-----------------------------------------------------
// comperation function for sorting
bool operator < ( const ID& a, const ID& b )
{
return a.score < b.score;
}
//-----------------------------------------------------
typedef deque<ID> Deque; // new name for existing datatype
int main ()
{
Deque d;
Deque::iterator Iter;
d.push_back(ID("Smith A",96));
d.push_back(ID("Amstrong B.",91));
d.push_back(ID("Watson D.",82));
for ( Iter = d.begin(); Iter != d.end(); Iter++ )
Iter->display();
sort(d.begin(),d.end()); // sort algorithm
cout << endl << "Sorted by Score" << endl;
cout << "===============" << endl;
for ( Iter = d.begin(); Iter != d.end(); Iter++ )
Iter->display();
cout << endl << "Reverse output" << endl;
cout << "===============" << endl;
Deque::reverse_iterator r = d.rbegin();
while ( r != d.rend() )
cout << r->display();
cout << endl;
return 0;
}
OUTPUT:
// 96 Smith A.
// 91 Amstrong B.
// 82 Watson D.
//
// Sorted by Score
// ===============
// 82 Watson D.
// 91 Amstrong B.
// 96 Smith A.
//
// Reverse output
// ===============
// 96 Smith A.
// 91 Amstrong B.
// 82 Watson D.
resize
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
deque<int> d(5);
for ( int i=0; i<5; i++ )
d[i] = i*2;
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
d.resize(7,100);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
d.resize(4);
copy(d.begin(),d.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
OUTPUT:
// 0 2 4 6 8
// 0 2 4 6 8 100 100
// 0 2 4 6
size
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
deque<char> d(5);
Print<char> print;
cout << "Size of d = " << d.size() << endl;
fill(d.begin(),d.end(),'*');
for_each(d.begin(),d.end(),print);
cout << endl;
for ( int i=0; i < d.size(); i++ )
cout << d[i] << " ";
cout << endl;
for ( int i=0; i<5; i++ )
{
cout << "Size of d = ";
for_each(d.begin(),d.end(),print);
cout << endl;
d.pop_back();
}
return 0;
}
OUTPUT:
// Size of d = 5
// * * * * *
// * * * * *
// Size of d = * * * * *
// Size of d = * * * *
// Size of d = * * *
// Size of d = * *
// Size of d = *
swap
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//=============================
int main ()
{
int ary[] = {1,2,3,4,5,6,7,8,9,10};
Print print;
deque<int> d1(ary,ary+7);
deque<int> d2(ary+7,ary+10);
cout << "Deque d1 : ";
for_each(d1.begin(),d1.end(),print);
cout << endl;
cout << "Size of d1 = " << d1.size()
<< endl << endl;
cout << "Deque d2 : ";
for_each(d2.begin(),d2.end(),print);
cout << endl;
cout << "Size of d2 = " << d2.size()
<< endl << endl;
d1.swap(d2);
cout << "After swapping:" << endl;
cout << "Deque d1 : ";
for_each(d1.begin(),d1.end(),print);
cout << endl;
cout << "Size of d1 = " << d1.size()
<< endl << endl;
cout << "Deque d2 : ";
for_each(d2.begin(),d2.end(),print);
cout << endl;
cout << "Size of d2 = " << d2.size()
<< endl << endl;
return 0;
}
OUTPUT:
// Deque d1 : 1 2 3 4 5 6 7
// Size of d1 = 7
//
// Deque d2 : 8 9 10
// Size of d2 = 3
//
// After swapping:
// Deque d1 : 8 9 10
// Size of d1 = 3
//
// Deque d2 : 1 2 3 4 5 6 7
// Size of d2 = 7