作者在 2008-08-17 14:16:44 发布以下内容
//Deque(dounle_ended queues]
#include<iostream>
#include<deque>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
int main(void)
{
deque<int> c1; //defauilt constructor
deque<int> c2(10,4); //create a deque with 10 copies of 4
deque<int> c3(c2); //copy constructor
int ai[] = {0,1,2,3,4,5};
deque<int> c4;
deque<int>::allocator_type a1 = c4.get_allocator(); //get_allocator
for ( int i=0;i<5;i++) c4.push_back(ai[i]); //push_back;
deque<int> c5(c4.begin(),c4.end()); //range copy constructor
cout<<"c4(using begin,end) = ";
deque<int>::iterator Iter;
for (Iter = c4.begin();Iter != c4.end();Iter++) cout<<*Iter<<",";
cout<<endl;
cout<<"c4(using rbegin,rend) = ";
deque<int>::reverse_iterator RevIter;
for (RevIter = c4.rbegin();RevIter != c4.rend();RevIter++) cout<<*RevIter<<",";
cout<<endl;
c2.assign(c5.begin(),c5.begin()+3); //assign
c1.assign(10,4);
cout<<"third element in c1 = "<<c1.at(3)<<endl; //at
cout<<"c4[3] = "<<c4[3]<<endl; //operator[]
cout<<"last element in c2 = "<<c2.back()<<endl; //back
cout<<"first element in c2 = "<<c2.front()<<endl; //front
cout<<"number of element in c2 = "<<c2.size()<<endl; //size
cout<<"max number of elements of c2 can hold using current allocator = "
<<c2.max_size()<<endl; //max_size
c3.erase(c3.begin(),c3.begin()+4); //erase
c2.clear(); //clear
if (c2.empty() == true) cout<<"now c2 is empty"<<endl;
c2.resize(10,30); //resize
cout<<"number of element in c2 = "<<c2.size()<<endl
<<"last element in c2 = "<<c2.back()<<endl
<<"first element in c2 = "<<c2.front()<<endl;
c2.push_front(25); //push_front
cout<<"First element in c2 = "<<c2.front()<<endl;
c2.pop_back(); //pop_back
cout<<"last element in c2 = "<<c2.back()<<endl;
c2.pop_front(); //pop_front;
cout<<"First element in c2 = "<<c2.front()<<endl;
c3.swap(c2); //swap
cout<<"number of elements in c3 = "<<c3.size()<<endl;
cout<<"last element in c3 = "<<c3.back()<<endl;
cout<<"first element in c3 = "<<c3.front()<<endl;
c1.insert(c1.begin(),20); //insert
c1.insert(c1.begin()+1,4,10);
c1.insert(c1.begin()+2,c5.begin(),c5.end());
cout<<"c1 = ";
for (Iter = c1.begin();Iter != c1.end();Iter++)
cout<<*Iter<<" ";
cout<<endl;
return 0;
}
#include<deque>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
int main(void)
{
deque<int> c1; //defauilt constructor
deque<int> c2(10,4); //create a deque with 10 copies of 4
deque<int> c3(c2); //copy constructor
int ai[] = {0,1,2,3,4,5};
deque<int> c4;
deque<int>::allocator_type a1 = c4.get_allocator(); //get_allocator
for ( int i=0;i<5;i++) c4.push_back(ai[i]); //push_back;
deque<int> c5(c4.begin(),c4.end()); //range copy constructor
cout<<"c4(using begin,end) = ";
deque<int>::iterator Iter;
for (Iter = c4.begin();Iter != c4.end();Iter++) cout<<*Iter<<",";
cout<<endl;
cout<<"c4(using rbegin,rend) = ";
deque<int>::reverse_iterator RevIter;
for (RevIter = c4.rbegin();RevIter != c4.rend();RevIter++) cout<<*RevIter<<",";
cout<<endl;
c2.assign(c5.begin(),c5.begin()+3); //assign
c1.assign(10,4);
cout<<"third element in c1 = "<<c1.at(3)<<endl; //at
cout<<"c4[3] = "<<c4[3]<<endl; //operator[]
cout<<"last element in c2 = "<<c2.back()<<endl; //back
cout<<"first element in c2 = "<<c2.front()<<endl; //front
cout<<"number of element in c2 = "<<c2.size()<<endl; //size
cout<<"max number of elements of c2 can hold using current allocator = "
<<c2.max_size()<<endl; //max_size
c3.erase(c3.begin(),c3.begin()+4); //erase
c2.clear(); //clear
if (c2.empty() == true) cout<<"now c2 is empty"<<endl;
c2.resize(10,30); //resize
cout<<"number of element in c2 = "<<c2.size()<<endl
<<"last element in c2 = "<<c2.back()<<endl
<<"first element in c2 = "<<c2.front()<<endl;
c2.push_front(25); //push_front
cout<<"First element in c2 = "<<c2.front()<<endl;
c2.pop_back(); //pop_back
cout<<"last element in c2 = "<<c2.back()<<endl;
c2.pop_front(); //pop_front;
cout<<"First element in c2 = "<<c2.front()<<endl;
c3.swap(c2); //swap
cout<<"number of elements in c3 = "<<c3.size()<<endl;
cout<<"last element in c3 = "<<c3.back()<<endl;
cout<<"first element in c3 = "<<c3.front()<<endl;
c1.insert(c1.begin(),20); //insert
c1.insert(c1.begin()+1,4,10);
c1.insert(c1.begin()+2,c5.begin(),c5.end());
cout<<"c1 = ";
for (Iter = c1.begin();Iter != c1.end();Iter++)
cout<<*Iter<<" ";
cout<<endl;
return 0;
}