📜  C++队列

📅  最后修改于: 2020-10-19 00:57:54             🧑  作者: Mango

C++队列

在计算机科学领域,我们致力于各种程序。他们每个人都有自己的域和实用程序。根据程序创建的目的和环境,我们有大量数据结构可供选择。其中之一就是“队列”。在讨论这种数据类型之前,让我们看一下它的语法。

句法

template > class queue;

此数据结构适用于FIFO技术,其中FIFO代表先进先出。首先插入的元素将首先被提取,依此类推。有一个称为“前”的元素,它是位于最前位置或说是第一个位置的元素,也有一个名为“后”的元素,它是位于最后位置的元素。在普通队列中,元素的插入在后端,而删除则从前端开始。

应用程序区域中的队列隐含为容器适配器。

容器应支持以下操作列表:

  • 空的
  • 尺寸
  • 推回
  • pop_front
  • 面前
  • 背部

模板参数

T:该参数指定容器适配器将保留的元素的类型。

容器:参数指定容器的内部对象,队列元素保留在该对象中。

会员类型

下面给出了队列成员类型的列表,并对其进行了简短描述。

Member Types Description
value_type Element type is specified.
container_type Underlying container type is specified.
size_type It specifies the size range of the elements.
reference It is a reference type of a container.
const_reference It is a reference type of a constant container.

功能

借助功能,可以在编程领域中使用对象或变量。队列提供了大量可以在程序中使用或嵌入的功能。相同的列表如下:

Function Description
(constructor) The function is used for the construction of a queue container.
empty The function is used to test for the emptiness of a queue. If the queue is empty the function returns true else false.
size The function returns the size of the queue container, which is a measure of the number of elements stored in the queue.
front The function is used to access the front element of the queue. The element plays a very important role as all the deletion operations are performed at the front element.
back The function is used to access the rear element of the queue. The element plays a very important role as all the insertion operations are performed at the rear element.
push The function is used for the insertion of a new element at the rear end of the queue.
pop The function is used for the deletion of element; the element in the queue is deleted from the front end.
emplace The function is used for insertion of new elements in the queue above the current rear element.
swap The function is used for interchanging the contents of two containers in reference.
relational operators The non member function specifies the relational operators that are needed for the queues.
uses allocator As the name suggests the non member function uses the allocator for the queues.

示例:一个简单的程序,显示基本队列功能的使用。

#include 
#include 
using namespace std;
void showsg(queue  sg)
{
    queue  ss = sg;
    while (!ss.empty())
    {
        cout << '\t' << ss.front();
        ss.pop();
    }
    cout << '\n';
}

int main()
{
    queue  fquiz;
    fquiz.push(10);
    fquiz.push(20);
    fquiz.push(30);

    cout << "The queue fquiz is : ";
    showsg(fquiz);

    cout << "\nfquiz.size() : " << fquiz.size();
    cout << "\nfquiz.front() : " << fquiz.front();
    cout << "\nfquiz.back() : " << fquiz.back();

    cout << "\nfquiz.pop() : ";
    fquiz.pop();
    showsg(fquiz);

    return 0;
}

输出:

The queue fquiz is :     10    20    30

fquiz.size() : 3
fquiz.front() : 10
fquiz.back() : 30
fquiz.pop() :     20    30