Learning Note【queue】

本文详细介绍了STL中队列的基本概念与使用方法,包括如何插入与移除元素,以及核心接口如push(), front(), back(), pop(), size(), empty()等的使用。并通过一个具体的例子展示了队列的操作流程。

queues are a type of container adaptor(容器适配器), specifically designed to operate in aFIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

==============================================================

要将数据加进queue时,只要用q.push(item)即可,但要取出数据时,并不是用q.pop(),而是用q.front()取出最前面的数据,但数据依然存在;q.pop()则是将最前面的数据取出queue,其回传值为void。

微笑一个栗子:

void testSTLQueue()
{
    queue<string> stringQueue;
    stringQueue.push("These ");
    stringQueue.push("are ");
    stringQueue.push("more ");
    cout<<stringQueue.front();
    stringQueue.pop();
    cout<<stringQueue.front();
    stringQueue.pop();
    stringQueue.push("four ");
    stringQueue.push("words!");
    stringQueue.pop();
    cout<<stringQueue.front();
    stringQueue.pop();
    cout<<stringQueue.front();
    stringQueue.pop();
}

输出结果是:There are four words!


核心接口:
1.push()
2.front()
3.back()

Returns a reference to the last element in thequeue. This is the "newest" element in the queue

4.pop()
5.size()
6.empty()

bool empty() const;//Test whether container is empty

7.comparision(const queue& q1,const queue& q2)//两个同型queue的比较,

comparision可以是:==,!=,<,>,<=,>=
(如果两个queue的size()相等并且对应元素也相等则它们相等;比较基于字典序,参考lexicographical_compare())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值