如何在 Visual C++ 中使用 STL 队列类的该成员函数

队列适配器包含队列所支持的容器的类型定义的类型的对象。 支持在两个容器是列表和在 deque。 对象并通过 push() 插入而 pop() 被删除。 front() 返回 (也称为 FIFO) 队列中的最早的项,并 back() 返回最新的项目在队列中插入。

 

Prototypes
----------

   queue::push();
   queue::pop();
   queue::empty();
   queue::back();
   queue::front();
   queue::size();


示例代码

//
//
// Compile options needed: none
//
// <filename> :  queue.cpp
//
// Functions:
//
//    queue::push(), queue::pop(), queue::empty(), queue::back(),
//    queue::front(),queue::size()
//
// Written by Debabrata Sarma
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//

/* Compile options needed: /GX
*/
#include <list>
#include <iostream>
#include <queue>
#include <deque>
using namespace std;

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

// Using queue with list

typedef list<int, allocator<int> > INTLIST;
typedef queue<int,INTLIST >  INTQUEUE;

// Using queue with deque

typedef deque<char*, allocator<char*> > CHARDEQUE;
typedef queue<char*,CHARDEQUE> CHARQUEUE;

void main(void)

{

    int size_q;
    INTQUEUE q;
    CHARQUEUE p;

    // Insert items in the queue(uses list)
    q.push(42);
    q.push(100);
    q.push(49);
    q.push(201);

    // Output the item inserted last using back()
    cout << q.back() << endl;

    // Output the size of queue
    size_q = q.size();
    cout << "size of q is:" << size_q << endl;

    // Output items in queue using front()
    // and use pop() to get to next item until
    // queue is empty
    while (!q.empty())
    {
        cout << q.front() << endl;
        q.pop();

    }

// Insert items in the queue(uses deque)
    p.push("cat");
    p.push("ape");
    p.push("dog");
    p.push("mouse");
    p.push("horse");

    // Output the item inserted last using back()
    cout << p.back() << endl;

    // Output the size of queue
    size_q = p.size();
    cout << "size of p is:" << size_q << endl;

    // Output items in queue using front()
    // and use pop() to get to next item until
    // queue is empty
    while (!p.empty())
    {
        cout << p.front() << endl;
        p.pop();

    }

}

 

程序输出:

201
size of q is:4
42
100
49
201
horse
size of p is:5
cat
ape
dog
mouse
horse

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值