元素类型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
queue
s 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.
queue
s 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
and
can be used. By default, if no container class is specified for a particular
queue
class, the standard container class template
is used.
In their implementation in the C++ Standard Template Library, queues take two template parameters:
|
|
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. ********/
文章结束给大家分享下程序员的一些笑话语录: 开发时间
项目经理: 如果我再给你一个人,那可以什么时候可以完工?程序员: 3个月吧!项目经理: 那给两个呢?程序员: 1个月吧!
项目经理: 那100呢?程序员: 1年吧!
项目经理: 那10000呢?程序员: 那我将永远无法完成任务.