STL deque的一些函数练习

5 篇文章 0 订阅
1 篇文章 0 订阅
#include <iostream>

#include<deque>

#include<vector>

using namespace std;



#if 0

int main()

{

/************************************************************************

    //deque构造函数

//    default (1) explicit deque (const allocator_type& alloc = allocator_type());

//    fill    (2) explicit deque (size_type n, const value_type& val = value_type(),

//                const allocator_type& alloc = allocator_type());

//    range   (3) template <class InputIterator>

//        deque (InputIterator first, InputIterator last,

//                const allocator_type& alloc = allocator_type());

//    copy    (4) deque (const deque& x);





    deque<int> first;//构造空的双端队列的int类型

    deque<int> second (4,100);  //构造int类型的4个100

    deque<int> third (second.begin(),second.end()-1);//用一个对象的一段去构造

    deque<int> fourth (third);///用一个对象去构造



    int myints[] = {16,2,77,29};

    deque<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );//用一个数组去构造

****************************************************************/

/***********************************************************

//    deque::assign()函数

//    range (1)   template <class InputIterator>

//                    void assign (InputIterator first, InputIterator last);

//

//    fill  (2)   void assign (size_type n, const value_type& val);

    //assign()函数就是从新初始化容器中的元素。

    deque<int> first;

    deque<int> second;

    deque<int> third;

    first.assign (7,100);//first中有7个100

    deque<int>::iterator it;

    it=first.begin()+1;

    second.assign (it,first.end()-1);//second中有5个100



    int myints[] = {1776,7,4};

    third.assign (myints,myints+3);//把数组mymints放到third容器中。

    it=third.begin();

    while(it!=third.end())

        cout<<*it++<<" ";

    //输出结果:1776 7 4

*************************************************/

    return 0;

}

#endif



#if 0

int main()

{

/*********************************************

//    deque::at()函数:

//    reference at (size_type n);

//    const_reference at (size_type n) const;

    deque<unsigned> mydeque (10);//初始化为10个0

    for (unsigned i=0; i<mydeque.size(); i++)

        mydeque.at(i)=i;

    for (unsigned i=0; i<mydeque.size(); i++)

        cout << ' ' << mydeque.at(i);

//    输出结果: 0 1 2 3 4 5 6 7 8 9

***********************************************/

/**********************************************

//    deque::back()函数:

//    reference back();

//    const_reference back() const;

    deque<int> mydeque;

    mydeque.push_back(10);//末尾加入一个元素

    while (mydeque.back() != 0) //返回末尾这个元素

        mydeque.push_back ( mydeque.back() -1 );

    for (deque<int>::iterator it = mydeque.begin(); it!=mydeque.end(); ++it)//通过迭代器访问容器中的元素

        cout << ' ' << *it;

//    输出结果: 10 9 8 7 6 5 4 3 2 1 0

*************************************************/

/******************************************

//    deque::clear()函数:

//    void clear();

    deque<int> mydeque;

    mydeque.push_back (100);

    mydeque.push_back (200);

    mydeque.push_back (300);

    for (deque<int>::iterator it = mydeque.begin(); it!=mydeque.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

//    输出结果:100 200 300

    mydeque.clear();//清空容器中的元素

    cout<<mydeque.size()<<endl;//输出0

********************************************/

 /*********************************   

//    deque::empty()函数:

//    bool empty() const;



    deque<int> mydeque;

    mydeque.empty();//判断容器是否为空,为空返回true,不空返回false

***********************************/

    return 0;

}

#endif

#if 0

int main()

{

/*********************************************************************

//    deque的erase()函数:

//    iterator erase (iterator position);

//    iterator erase (iterator first, iterator last);

    deque<int> mydeque;

    for (int i=1; i<=10; i++)

        mydeque.push_back(i);//尾部插入元素

    mydeque.erase (mydeque.begin()+5);//删除指定ierator的元素

    mydeque.erase (mydeque.begin(),mydeque.begin()+3);//删除一段数据

***********************************************************************/

/***************************************

//    deque::front():

//    reference front();

//    const_reference front() const;



    deque<int> mydeque;

    mydeque.push_front(2);//头部插入元素

    mydeque.push_front(10);

    cout<<mydeque.front();//输出10

**************************************/



//    deque::insert()函数:

//    (1)iterator insert (iterator position, const value_type& val);

//    (2)void insert (iterator position, size_type n, const value_type& val);

//    (3)template <class InputIterator>

//        void insert (iterator position, InputIterator first, InputIterator last);

    deque<int> mydeque;

    for (int i=1; i<6; i++) // 1 2 3 4 5

        mydeque.push_back(i);

    deque<int>::iterator it = mydeque.begin();

    ++it;

    it = mydeque.insert (it,10);//在it位置插入10.现在容器中元素是 1 10 2 3 4 5



    mydeque.insert (it,2,20);//在it位置插入2个20,现在容器中元素是1 20 20 10 2 3 4 5



    it = mydeque.begin()+2;

    vector<int> myvector (2,30);

    mydeque.insert (it,myvector.begin(),myvector.end());//现在容器中元素是 1 20 30 30 20 10 2 3 4 5



    for (deque<int>::iterator it = mydeque.begin(); it!=mydeque.end(); ++it)

        cout << ' ' << *it;

    return 0;

}

#endif // 1

deque函数的用法和vector基本类似,因此只是写了一点

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值