zoukankan      html  css  js  c++  java
  • C++ 标准模板库STL 队列 queue 使用方法与应用介绍

    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.

  • 相关阅读:
    tabbar 旋转指定的页面
    GDAL中文路径不能打开&Shp文件字段属性值中文乱码
    Project : error PRJ0019: 工具从"Moc'ing xxx.h..."
    详解Android中的屏幕方向
    qt中获取文件路径和文件名
    vs2005下Qt项目中修改exe图标的方法
    Qt & C/C++统计运行时间
    Qt 中Treewidget添加右键菜单
    QT 中文乱码解决方案
    Qt多线程应用QRunnable显示进度条示例
  • 原文地址:https://www.cnblogs.com/dingxiaoqiang/p/7120350.html
Copyright © 2011-2022 走看看