cpp_std_queue
jiaxw32 edited this page Jul 19, 2020
·
2 revisions
模版定义
template <class Type, class Container = deque <Type>>
class queue
- Type: 要存储在队列中的元素数据类型
- Container: 用来实现队列的基础容器的类型。
- Queue 对象的第一个模板参数中的类 Type 规定的元素与 value_type 同义,并且必须与第二个模板参数 Container 规定的基础容器类中的元素类型相匹配。
- 适用于队列的适当基础容器类包括 deque 和 list,或支持 front、back、push_back和 pop_front操作的任何其他序列容器。
queue();
explicit queue(const container_type& right);
- right: 要以构造的队列为副本的 const 容器。
队列的默认基容器是 deque。 还可以指定列表作为基容器,但不能指定矢量,因为它缺少所需的 pop_front 成员函数。
示例:
#include <queue>
#include <vector>
#include <list>
#include <iostream>
int main( )
{
using namespace std;
// Declares queue with default deque base container
queue <char> q1;
// Explicitly declares a queue with deque base container
queue <char, deque<char> > q2;
// Declares a queue with list base container
queue <int, list<int> > q4;
// The second member function copies elements from a container
list<int> li1;
li1.push_back( 1 );
li1.push_back( 2 );
queue <int, list<int> > q5( li1 );
cout << "The element at the front of queue q5 is "
<< q5.front( ) << "." << endl; //output: 1
cout << "The element at the back of queue q5 is "
<< q5.back( ) << "." << endl; //output: 2
}
返回队列中的元素数目
size_type size() const;
示例:
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1, q2;
queue <int>::size_type i;
q1.push( 1 );
i = q1.size( );
cout << "The queue length is " << i << "." << endl; //output: 1
q1.push( 2 );
i = q1.size( );
cout << "The queue length is now " << i << "." << endl; //output: 2
}
一种无符号整数类型,此类型可表示队列中的元素数量。
typedef typename Container::size_type size_type;
将元素添加到队列的后部
void push(const Type& val);
示例:
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1;
q1.push( 10 );
q1.push( 20 );
q1.push( 30 );
queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl; //output: 3
i = q1.front( );
cout << "The element at the front of the queue is "
<< i << "." << endl; //output: 10
}
从队列的前部移除元素。
void pop();
示例:
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1, s2;
q1.push( 10 );
q1.push( 20 );
q1.push( 30 );
queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl; //output: 3
i = q1.front( );
cout << "The element at the front of the queue is "
<< i << "." << endl; //output: 10
q1.pop( );
i = q1.size( );
cout << "After a pop the queue length is "
<< i << "." << endl; //output: 2
i = q1. front ( );
cout << "After a pop, the element at the front of the queue is "
<< i << "." << endl; //output: 20
}
返回对队列前部的第一个元素的引用。
reference front();
const_reference front() const;
如果将 front 的返回值分配给 const_reference,则无法修改队列对象。 如果 front 的返回值分配给 reference,则可以修改队列对象。
示例:
#include <queue>
#include <iostream>
int main() {
using namespace std;
queue <int> q1;
q1.push( 10 );
q1.push( 20 );
q1.push( 30 );
queue <int>::size_type i;
i = q1.size( );
cout << "The queue length is " << i << "." << endl;
int& ii = q1.back( );
int& iii = q1.front( );
cout << "The integer at the back of queue q1 is " << ii
<< "." << endl;
cout << "The integer at the front of queue q1 is " << iii
<< "." << endl;
}
返回对在队列后部最近添加的最后一个元素的引用。
reference back();
const_reference back() const;
如果将 back 的返回值分配给 const_reference,则无法修改队列对象。 如果 back 的返回值分配给 reference,则可以修改队列对象。
示例:
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
queue <int> q1;
q1.push( 10 );
q1.push( 11 );
int& i = q1.back( );
const int& ii = q1.front( );
cout << "The integer at the back of queue q1 is " << i
<< "." << endl;
cout << "The integer at the front of queue q1 is " << ii
<< "." << endl;
}
测试队列是否为空,如果队列为空,则为 true;如果队列不为空,则为 false。
bool empty() const;
示例:
#include <queue>
#include <iostream>
int main( )
{
using namespace std;
// Declares queues with default deque base container
queue <int> q1, q2;
q1.push( 1 );
if ( q1.empty( ) )
cout << "The queue q1 is empty." << endl;
else
cout << "The queue q1 is not empty." << endl;
if ( q2.empty( ) )
cout << "The queue q2 is empty." << endl;
else
cout << "The queue q2 is not empty." << endl;
}
- Microsoft C++ 标准库
- 《侯捷 C++ 标准库体系结构与内核分析》