C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)

queue

queue模板类的定义在<queue>头文件中。

与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque类型。

定义queue对象的示例代码如下:

queue<int> q1;

queue<double> q2;

queue的基本操作有:


入队,如例:q.push(x); 将x接到队列的末端。

出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

访问队首元素,如例:q.front(),即最早被压入队列的元素。

访问队尾元素,如例:q.back(),即最后被压入队列的元素。

判断队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()


std::queue

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

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:

  • front()
  • back()
  • push_back()
  • pop_front()

Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

In their implementation in the C++ Standard Template Library, queues take two template parameters:
 
template < class T, class Container = deque<T> > class queue;

Where the template parameters have the following meanings:
  • T: Type of the elements.
  • Container: Type of the underlying container object used to store and access the elements.
In the reference for the queue member functions, these same names are assumed for the template parameters.

 


#include <iostream>
#include <queue>
#include <string>
using namespace std;
void test_empty()
{
  queue<int> myqueue;
  int sum (0);

  for (int i=1;i<=10;i++) myqueue.push(i);

  while (!myqueue.empty())
  {
     sum += myqueue.front();
     myqueue.pop();
  }
    cout << "total: " << sum << endl;
}//运行结果: total: 55
void test_pop()
{
  queue<int> myqueue;
  int myint;

  cout << "\nPlease enter some integers (enter 0 to end):\n";

  do
  {
    cin >> myint;
    myqueue.push (myint);
  } while (myint);

  cout << "myqueue contains: ";
  while (!myqueue.empty())
  {
    cout << " " << myqueue.front();
    myqueue.pop();
  }
}
/********
运行结果:
Please enter some integers (enter 0 to end):
512
605
420
517
532
0
myqueue contains:  512 605 420 517 532 0
********/
void test_size()
{
  queue<int> myints;
  cout << "0. size: " << (int) myints.size() << endl;

  for (int i=0; i<5; i++) myints.push(i);
  cout << "1. size: " << (int) myints.size() << endl;

  myints.pop();
  cout << "2. size: " << (int) myints.size() << endl;
}
/****
运行结果:
0. size: 0
1. size: 5
2. size: 4
****/
int main()
{
    test_empty();
    cout<<"\n***********************************************\n";
    test_size();
    cout<<"\n***********************************************\n";
    test_pop();
    cout<<"\n***********************************************\n";
    queue<string> q;

    // insert three elements into the queue
    q.push("These ");
    q.push("are ");
    q.push("more than ");
    //cout << "number of elements in the queue: " << q.size()<< endl;

    // read and print two elements from the queue
    cout << q.front();
    q.pop();
    cout << q.front();
    q.pop();
    //cout << "number of elements in the queue: " << q.size()<< endl;

    // insert two new elements
    q.push("four ");
    q.push("words!");
    //cout << "\nnumber of elements in the queue: " << q.size()<< endl;
    // skip one element
    q.pop();

    // read and print two elements
    cout << q.front();
    q.pop();
    cout << q.front() << endl;
    q.pop();

    // print number of elements in the queue
    cout << "number of elements in the queue: " << q.size()<< endl;
}
/*******
*运行结果:
total: 55

***********************************************
0. size: 0
1. size: 5
2. size: 4

***********************************************

Please enter some integers (enter 0 to end):
512
605
420
517
532
0
myqueue contains:  512 605 420 517 532 0
***********************************************
These are four words!
number of elements in the queue: 0

Process returned 0 (0x0)   execution time : 33.512 s
Press any key to continue.

********/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值