C++ STL queue介绍与使用方法

queue(队列)

队列也是一种逻辑数据结构,其具有先进先出的特性,针对这种特性,可以实现一些较为复杂的逻辑。在实际应用中,部分程序也正需要这样一种顺序进出的数据处理方式。使用这样的逻辑处理方式,使得我们可以将更多精力放在如何处理顺序逻辑之外的事情,对于编程、开发来讲,提供了极大的方便。

同stack类似,queue也可以看成是容器的容器,内部是使用其它容器来存放具体数据。加了一个外壳,使得我们的数据操作只能是在头或尾。从尾部添加数据,从头部取数据,从而实现FIFO的特性。同stack一样,内部默认数据存放容器为deque,若要用非默认容器初始化,必须要在模板中指定容器类型。

构造函数
std::deque<int> mydeck (3,100);        // deque with 3 elements
std::list<int> mylist (2,200);         // list with 2 elements

std::queue<int> first;                 // empty queue
std::queue<int> second (mydeck);       // queue initialized to copy of deque

std::queue<int,std::list<int> > third; // empty queue with list as underlying container
std::queue<int,std::list<int> > fourth (mylist);

std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n';
常用函数
std::deque<int> mydeck (3,100);    // deque with 3 elements
std::list<int> mylist (2,200);     // list with 2 elements

std::queue<int> first;  // empty queue
std::queue<int> second (mydeck);    // queue initialized to copy of deque


first.empty();   //队列是否为空
int size;
size = first.size();    //队列元素个数
int n;
n = first.front();   //返回队头元素
n = first.back();    //返回队尾元素
first.push(20);      //向队列添加元素,添加到队尾
first.pop();         //删除下一个元素,也即队头元素
队列应用
  • 应用一:用队列实现栈
#include <iostream>
#include <queue>
#include <list>
#include <deque>

using namespace std;

//用一个队列实现栈
template<class T>
class MyNewStack{

private:
    int count;
    queue<T> myQueue;

public:
    MyNewStack() :count(0)
    {

    }

    int size()
    {
        return count;
    }

    void push(T element)
    {
        count++;
        myQueue.push(element);
    }

    T top()
    {
        return myQueue.back();
    }

    void pop()
    {
        int tmp;
        for (int i = 0; i < count - 1; i++)
        {
            tmp = myQueue.front();
            myQueue.pop();
            myQueue.push(tmp);
        }

        myQueue.pop();

    }

};

int main()

    //利用一个queue实现stack
    MyNewStack<int> myStack;

    myStack.push(10);
    myStack.push(20);
    myStack.push(30);

    cout << myStack.top() << endl;
    cout << myStack.top() << endl;
    myStack.pop();
    cout << myStack.top() << endl;

    myStack.push(40);
    cout << myStack.top() << endl;

    return 0;
}
  • 应用二:图的广度优先遍历(BFS)

#include <iostream>
#include <queue>

using namespace std;

//无向图图的广度优先遍历(BFS) 
class GraphBFS{

    int ** data;  //邻接矩阵存储图
    int * flag;   //标记结点是否已经访问
    int N;        //结点个数

public:
    GraphBFS() :data(NULL)
    {

    }

    void setData(int **arr, int n)
    {
        data = new int*[n];
        for (int i = 0; i < n; i++)
        {
            data[i] = new int[n];           
            memcpy(data[i], (arr+i), sizeof(int)*n);
        }

        this->N = n;

        flag = new int[N];
        memset(flag, 0, sizeof(int)*N);


    }

    //广度优先遍历
    void BFSTraverse(int start)
    {
        if (data == NULL)
        {
            cout << "There is no graph data" << endl;
        }
        else
        {
            //寻找起始点
            queue<int>  myQueue;
            cout << start << endl;  //访问起始结点
            myQueue.push(start);

            while (!myQueue.empty())
            {
                int tmp = myQueue.front();
                myQueue.pop();

                //寻找邻接结点,并访问
                for (int i = tmp+1; i < N; i++)
                {
                    if (data[tmp][i] == 1 && flag[i] == 0)
                    {

                        cout << i << endl;   //访问邻接结点
                        flag[i] = 1;         //标记为已访问
                        myQueue.push(i);
                    }
                }

            }
        }

    }

};


int main()
{
    //图的广度遍历
    int data[6][6] = {
        0, 1, 1, 1, 0, 0,
        1, 0, 0, 1, 0, 0,
        1, 0, 0, 0, 0, 0,
        1, 1, 0, 0, 1, 0,
        0, 0, 0, 1, 0, 1,
        0, 0, 0, 0, 1, 0
    };

    GraphBFS graphBFS;
    graphBFS.setData((int**)data, 6);
    graphBFS.BFSTraverse(0);

    return 0;
}


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的代码展示了使用STL库中的queue容器的示例。queue是一种先进先出(FIFO)的数据结构,它允许在队尾插入元素,在队首删除元素。在这个示例中,首先创建了一个CData的对象test1,并将其加入到了dataQueue中,然后通过front()函数获取dataQueue的队首元素并赋值给data对象,最后通过pop()函数将队首元素从dataQueue中弹出。 引用说明了queue的底层实现类型,默认为std::deque,但也可以是其他类型,只要支持相应的操作,如front、back、push_back和pop_front等。 引用列举了queue常用的成员函数和功能。其中,empty()函数用于判断queue是否为空,size()函数用于返回queue中元素的个数,front()函数返回queue的第一个元素的引用,back()函数返回queue的最后一个元素的引用,push()函数用于在queue的尾部添加一个元素的副本,emplace()函数用于在queue的尾部直接添加一个元素,push(T&& obj)函数以移动的方式在queue的尾部添加元素,pop()函数用于删除queue中的第一个元素,swap()函数用于交换两个queue容器的元素。 综上所述,引用的代码示例展示了使用STL库中的queue容器的基本操作,包括插入、删除和访问元素的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [c++ stl queue使用](https://blog.csdn.net/tianyexing2008/article/details/126356034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++STLqueue的简单使用](https://blog.csdn.net/lady_killer9/article/details/79261798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值