关闭

STL中的deque容器

331人阅读 评论(0) 收藏 举报

deque全称为“double-ended queue”,是一种动态数组形式,是一种随机访问的数据类型,是一种双端队列容器。它的重要特征就是在deque的两端放置元素和删除元素是高效的,而vector对象只有在其尾部执行操作时才有较高的效率。deque有五中初始化方式:

1.deque<A> name; 2.deque<A>name(size); 3.deque<A>name(size,value); 4.deque<A>name(mydeque); 5.deque<A>name(first, last);

#include <string>
#include <deque>
#include <iostream>
#include<algorithm>
void main (void) 
{  std:: deque<std::string> Mydeque;
Mydeque.push_back("good!");
Mydeque.insert(find(Mydeque.begin(),Mydeque.end(),"good!"),"YCL");
copy(Mydeque.begin(),Mydeque.end(),std::ostream_iterator<std::string>(std::cout," "));
}

///////////////遍历deque元素////////////////////

#include <iostream>
#include <deque>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
int main()
{
deque<int> myDeque(10,1);
int x=0;
deque<int>::iterator iter;
for(iter=myDeque.begin(); iter!=myDeque.end();iter++)
{ cout<<"Element "<<x++<<":"<<*iter<<endl;}
return 1;
}

////////////////////deque赋值///////////////////////////

#include <iostream>
#include <deque>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
typedef deque<int, allocator<int> >  INTDEQUE;
void printcontents (INTDEQUE  deque);
void main()
{
INTDEQUE  dequetest;
    dequetest.push_front(1);
    dequetest.push_front(2);
    dequetest.push_front(3);
    printcontents (dequetest);
    dequetest.pop_front();
    printcontents (dequetest);
    dequetest.pop_back();
    printcontents (dequetest);
}
void printcontents (INTDEQUE  deque)
{
    INTDEQUE::iterator pdeque;
    cout <<"The output is:"<<endl;
    for(pdeque = deque.begin(); pdeque != deque.end(); pdeque++)
    {    cout << *pdeque <<" " ; };
cout <<endl;
}

//////////////////////////////////内容交换,有两种交换方式,看下面实例//////////////////////////////////

#include <deque>
#include <iostream>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
int main( ) 
{
   deque <int> c1, c2, c3;
   deque <int>::iterator c1_Iter;
   c1.push_back( 1 );
   c1.push_back( 2 );
   c1.push_back( 3 );
   c2.push_back( 10 );
   c2.push_back( 20 );
   c3.push_back( 100 );
   cout << "The original deque c1 is: ";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << *c1_Iter << " ";
   cout << endl;
   c1.swap( c2 ); // 把c1和 c2两个容器中的内容进行相互交换,这是其中一种交换方法,swap当私有变量
   cout << "After swapping with c2, deque c1 is: ";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << *c1_Iter << " ";
   cout << endl;
   swap( c1,c3 ); // 把c1和 c3两个容器中的内容进行相互交换,另一种交换方法,swap当通用算法
   cout << "After swapping with c3, deque c1 is: ";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << *c1_Iter << " ";
   cout << endl;
   return 0;
}

//////////////////////通过重置assign也可以实现内同交换//////////////////////////

#include <iostream>
#include <deque>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
typedef deque<char >  CHARDEQUE;
void print_contents (CHARDEQUE  deque, char*);
int main()
{
    CHARDEQUE  a(3,'A'); //定义了变量a,内容是3个'A'
    CHARDEQUE  b(4,'B'); //定义了变量b,内容是4个'B'
    //print_contents (a,"a");
   // print_contents (b,"b");
    a.swap(b); //内容互换
   // print_contents (a,"a");
   // print_contents (b,"b");
    a.swap(b);//内容换回来
    print_contents (a,"a");
    print_contents (b,"b");
    a.assign(b.begin(),b.end());
    print_contents (a,"a");
    a.assign(b.begin(),b.begin()+2);
    print_contents (a,"a");
     a.assign(3,'Z');
    print_contents (a,"a");
return 0;
}
//下面是显示函数源代码
void print_contents (CHARDEQUE  deque, char *name)
{
    CHARDEQUE::iterator pdeque;
    cout <<"The contents of "<< name <<" : ";
        for(pdeque = deque.begin();
        pdeque != deque.end();
        pdeque++)
    {
        cout << *pdeque <<" " ;
    }
    cout<<endl;
}

///////////////////////////////////////////////////////////插入insert操作,有三种方式/////////////////////////////////////////////////////////////////

#include <iostream>
#include <deque>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
typedef deque<char* >  CHARDEQUE;
void print_contents (CHARDEQUE  deque);
int main()
{   CHARDEQUE  a(3,"A");
    CHARDEQUE  b(2,"B");
    print_contents (a);
    print_contents (b);
    a.insert(a.begin(),"X");  // 在deque的头部插入一元素,值为'X',第一种方式
    print_contents (a);
    a.insert(a.end(),"Rlong"); // 在deque的尾部插入一元素,值为'Y'
    print_contents (a);
    //inset 3 'Z's to one item before the end of a
    a.insert(a.end()-1,3,"Z"); // 在deque的尾部前一元素处插入3个元素,值为'Z',第二中方式
    print_contents (a);
    a.insert(a.end(),b.begin(),b.end()); // 把b的所有值插入到a变量的尾部,第三种方式
    print_contents (a);
return 0;
}
//deque的显示函数
void print_contents (CHARDEQUE  deque)
{   CHARDEQUE::iterator pdeque;
    cout <<"The output is: ";
    for(pdeque = deque.begin(); pdeque != deque.end(); pdeque++)
    {     cout << *pdeque <<" " ;    }
    cout<<endl;
}

///////////////////////////////////erase的两种方式///////////////////////////////////////

#include <deque>
#include <iostream>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
int main( ) 
{  using namespace std;
   deque <int> c1;
   deque <int>::iterator Iter;
   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c1.push_back( 40 );
   c1.push_back( 50 );
   cout << "The initial deque is: ";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;
   c1.erase( c1.begin( ) );                 //第一种方式
   cout << "After erasing the first element, the deque becomes:  ";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;
   Iter = c1.begin( );
   Iter++;
   c1.erase( Iter, c1.end( ) );                         //第二种方式
   cout << "After erasing all elements but the first, deque becomes: ";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;
return 0;
}

//////////////////////////////////////////////////////////////////////

除了可以用erase删除某一特定元素或者某一个范围的元素外,STL还提供了另外一个函数clear,它的功能完全可以被erase替换,等效与erase(deque::begin(),deque::end())

如果deque存储的是类对象,这时一般不能采用clear()函数,因为clear()不会自动释放原来对象元素所占用的内存。这时可以使用erase或者pop_back()辅助delete语句进行内存释放和元素删除操作。

#include <deque>
#include <iostream>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
int main( ) 
{  using namespace std;
   deque <int> c1;
   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   cout << "The size of the deque is initially " << c1.size( ) << endl;
   c1.clear();
   cout << "The size of the deque after clearing is " << c1.size( ) << endl;
   return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6148次
    • 积分:196
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:7篇
    • 译文:0篇
    • 评论:0条