• Queue队列详解


    概述

    在STL中由Class queue<>实作出一个queue(队列,FIFO, 先进先出)。我们可以使用push()将任意元素数量置入queue,使用pop()将元素依其插入次序从容器中移除,queue是个典型的数据缓冲结构;queue接口示意图如图1.

    为了运用queue,我们需要包含头文件<queue>,class queue定义如下:

    namespace std
    {
    	template<class T,
    			 class Container = deque<T> >
    	class queue;
    }
    第一个template参数代表元素型别,第二个参数用来决定用何种容器来存放元素,默认是使用queue作为实际的容器。

    实际上queue只开放了符合queue结构的内部容器接口,其余的均未定义,因此queue的各项操作都转化为内部容器的对应接口,如图2所示。

    核心接口

    push()

    • 将一个元素置入queue中,该元素成为其最后的元素

    pop()

    • 移除queue中的第一元素
    • 此函数无返回值,想处理被移除的元素,必须先调用front()
    • 调用者保证queue非空

    front()

    • 返回第一个被置入的元素,即返回queue最前端的元素
    • 调用者保证queue不空

    back()

    • 返回最后一个被插入的元素
    • 调用者保证queue非空

    源码实现

    STL中的queue实现源码如下,我们也可以根据自己需求重新实现queue的实作版本,相关实现方式可参考Stacks原理剖析一文。

    // TEMPLATE CLASS queue
    template<class _Ty,class _Container = deque<_Ty> >
    class queue
    {	// FIFO queue implemented with a container
    public:
        typedef _Container container_type;
        typedef typename _Container::value_type value_type;
        typedef typename _Container::size_type size_type;
        typedef typename _Container::reference reference;
        typedef typename _Container::const_reference const_reference;
    
        queue() : c()
        {	// construct with empty container
        }
    
        explicit queue(const _Container& _Cont) : c(_Cont)
        {	// construct by copying specified container
        }
    
        bool empty() const
        {	// test if queue is empty
            return (c.empty());
        }
    
        size_type size() const
        {	// return length of queue
            return (c.size());
        }
    
        reference front()
        {	// return first element of mutable queue
            return (c.front());
        }
    
        const_reference front() const
        {	// return first element of nonmutable queue
            return (c.front());
        }
    
        reference back()
        {	// return last element of mutable queue
            return (c.back());
        }
    
        const_reference back() const
        {	// return last element of nonmutable queue
            return (c.back());
        }
    
        void push(const value_type& _Val)
        {	// insert element at beginning
            c.push_back(_Val);
        }
    
        void pop()
        {	// erase element at end
            c.pop_front();
        }
    
        const _Container& _Get_container() const
        {	// get reference to container
            return (c);
        }
    
    protected:
        _Container c;	// the underlying container
    };

    queue实例

    /****************************************************************
    *函数名称:QueueTest
    *功    能:普通队列操作示例
    *作    者:Jin
    *日    期:2016年7月6日
    ****************************************************************/
    void QueueTest()
    {
        queue<string> strQueue;
        
        //insert three element into the queue
        strQueue.push("Whatever is worth doing is worth doing well.");
        strQueue.push("The hard part isn’t making the decision. It’s living with it.");
        strQueue.push("there are more than four words!");
    
        while (!strQueue.empty())
        {
            cout << strQueue.front() << endl;
            strQueue.pop();
        }
    }

    输出

    依次打印push到queue中的语句



  • 相关阅读:
    更改文件默认打开方式
    python数据分析高频词提取,pyecharts词云制作并保存
    pyecharts V1.x版本使用Map绘制地图修改主题背景色等
    设置随机请求头和使用代理
    【重学前端】JS基础-原型和原型链
    【重学前端】JS基础-变量和类型
    Bootstrap blog整页制作
    拉勾网 移动端流式布局与rem布局整页制作
    PC端管理后台整页制作
    QQ飞车官方首页(部分)制作
  • 原文地址:https://www.cnblogs.com/jinxiang1224/p/8468405.html
Copyright © 2020-2023  润新知