当前位置:网站首页 > 创业 > 正文

C++:queue队列的使用

0 张子豪 张子豪 2025-10-12 04:30 1

绪:

看法式碰到queue队列的利用方式;

下面经由过程一个例程来简要概述一下queue队列的常识点:

什么是队列;挨次队列;

C++:queue队列的用法;

模板类;

东西/原料

  • Visual Studio 2010

方式/步调

  1. 1

    queue应用例程:

    #include <queue>

    #include <iostream>

    using namespace std;

    int main()

           queue<int> myQ; 

           for(int i=0; i<10; i++) 

                  myQ.push(i);

           cout<<"myQ size is: "<<myQ.size()<<endl;

           for(int i=0; i<myQ.size(); i++) 

           { 

                  cout << myQ.front()<<endl; 

                  myQ.pop(); 

           } 

           cout<<"myQ size is: "<<myQ.size()<<endl;

           return 0;

    }

  2. 2

    什么是队列?

    队列是一种特别的线性表,

    特别之处在于:它只许可在表的前端进行删除操作,只许可在表的后端进行插入操作;

    队列是一种操作受限制的线性表;

    进行插入操作的端称为队从头至尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

    队列的数据元素又称为队列元素。

    在队列中插入一个队列元素称为入队,

    从队列中删除一个队列元素称为出队。

    因为队列只许可在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为进步前辈先出(FIFO—first in first out)线性表。

  3. 3

    挨次队列:

    成立挨次队列布局必需为其静态分派或动态申请一片持续的存储空间,并设置两个指针进行办理。

    一个是队头指针front,它指标的目的队头元素;

    另一个是队从头至尾指针rear,它指标的目的下一个入队元素的存储位置,

    如图所示:

  4. 4

    C++中的queue:

    queue是STL的队列,有FIFO的特征。

    ①队列头文件:#include <queue>

    ②queue模板类:需要两个模板参数,

    一个是元素类型,一个容器类型,

    元素类型是需要的,容器类型是可选的,默认为deque类型。

    界说queue对象的示例代码如下:

    queue<int> q1;

    queue<double> q2;

    queue<Point> q3;

  5. 5

    queue的根基操作有:

    入队,如例:q.push(x); 将x接到队列的结尾。

    出队,如例:q.pop(); 弹出队列的第一个元素,注重,并不会返回被弹出元素的值。

    拜候队首元素,如例:q.front(),即最早被压入队列的元素。

    拜候队从头至尾元素,如例:q.back(),即最后被压入队列的元素。

    判定队列空,如例:q.empty(),当队列空时,返回true。

    拜候队列中的元素个数,如例:q.size()

  6. 6

    queue的应用:

    #include "stdafx.h" 

    #include <queue>

    #include <iostream>

    #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; 

    }

    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(); 

           } 

     

    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; 

     

    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!"); 

           // skip one element 

           q.pop(); 

     

           // read and print two elements 

           cout << q.front(); 

           q.pop(); 

           cout << q.front() << endl; 

           q.pop(); 

           cout << "number of elements in the queue: " << q.size()<< endl; 

           return 0;

    }  

注重事项

  • q.push(x); 将x接到队列的结尾
  • q.pop(); 弹出队列的第一个元素,注重,并不会返回被弹出元素的值。
  • q.front()拜候首元素;
  • q.back()拜候从头至尾元素;

来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!


本文链接:https://www.ibaiwen.com/web/236464.html

张子豪

张子豪

TA很懒,啥都没写...

@百闻娱乐 本站部分内容转自互联网,若有侵权等问题请及时与本站联系,我们将在第一时间删除处理。 | 粤ICP备2024343649号 | (地图