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.
[cpp]
#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.
********/
#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.
********/