queue-的常见操作

#include<iostream>
#include <queue>
#include <list>

using namespace std;
#if 0
*   1.队列是一种容器适配器, 用于在FIFO上下文中进行操作, 其中元素从一段插入, 然后在另一端进行提取;
*   2.队列实现为容器适配器, 使用特定容器类的封装对象作为其底层容器的类, 提供一些特定成员来访问其中的元素, 元素被推入特定容器的后面, 并且从容器的前面弹出;
*   3.构造函数通常包括一下几种:
*   1.explict queue(const container_type& ctnr)
*   2.explict queue(container_type&& ctnr = container_type());
*   3.template <class Alloc> explict queue(const Alloc& alloc);
*   4.template <class Alloc> explict queue(const container_type& cntr, const Alloc& alloc);
*   5.template <class Alloc> explict queue(container_type&& cntr, const Alloc &alloc);
*   6.template <class Alloc> explict queue(const queue& X, const Alloc& alloc);
*   7.template <class Alloc> explict queue(queue&& X, const Alloc& alloc);
#endif

int main()
{
    //construct object;
    deque<int> mydeque(3, 33);
    list<int> mylist(2, 11);

    queue<int> first;
    first.push(3);
    queue<int> second(mydeque);
    queue<int, list<int>> third;
    queue<int, list<int>> fourth(mylist);
    cout << "first.size: " << first.size() << endl;
    cout << "second.size: " << second .size() << endl;
    cout << "third size: " << third.size() << endl;
    cout << "fourth.size: " << fourth.size() << endl;

    //.back():用于返回当前队列的最后一个元素;
    // reference& back();
    // const_reference& back() const;
    cout << "first.back: " << first.back() << endl;

#if 1
    //.front()用于返回第一个元素
    //reference& front();
    //const_reference& front() const;

    first.front() += first.back();
    cout << "first.front: " << first.front() << endl;

    //.emplace(): template <class... args> void emplace(Args&&.. args)
    //用于在队列尾部添加一个新的元素;

    queue<string> myqueuestring;
    myqueuestring.emplace("this is a freshman");
    myqueuestring.emplace("this is a oldman");
    cout << "myqueuestring constains: ";
    while (!myqueuestring.empty())
    {
        cout << myqueuestring.front() << "   ";
        myqueuestring.pop();
    }
    cout << endl;
    //.push用于在一段放入元素;
    //void push(const value_type& val);
    //void push(value_type&& val);
    myqueuestring.push("that is a no-name man");
    //.size()用于返回队列里面元素的多少;

    //.swap():用于交换两个队列的值;
    queue<int> newfirst;
    newfirst.push(1);
    newfirst.push(2);
    newfirst.push(3);
    newfirst.push(4);
    newfirst.push(5);
    queue<int> newsecond;
    newsecond.push(5);
    newsecond.push(4);
    newsecond.push(3);
    newsecond.push(2);
    newsecond.push(1);

    cout << "newfirst contains: ";
    while (!newfirst.empty())
    {
        cout << newfirst.front() << " ";
        newfirst.pop();
    }
    cout << endl;
    cout << "newsecond contains: ";
    while (!newsecond.empty())
    {
        cout << newsecond.front() << " ";
        newsecond.pop();
    }
    cout << endl;
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值