cqueue结构pop_Queue队列详解

移除queue中的第一元素

此函数无返回值,想处理被移除的元素,必须先调用front()

调用者保证queue非空

front()

返回第一个被置入的元素,即返回queue最前端的元素

调用者保证queue不空

back()

返回最后一个被插入的元素

调用者保证queue非空

源码实现

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

// TEMPLATE CLASS queue

template >

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 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中的语句

590535cfa8f97f1e00f0c87797a8738f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值