C++:queue队列的使用

绪:

看法式碰到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()拜候从头至尾元素;
  • 发表于 2018-05-08 00:00
  • 阅读 ( 971 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

0 篇文章

作家榜 »

  1. xiaonan123 189 文章
  2. 汤依妹儿 97 文章
  3. luogf229 46 文章
  4. jy02406749 45 文章
  5. 小凡 34 文章
  6. Daisy萌 32 文章
  7. 我的QQ3117863681 24 文章
  8. 华志健 23 文章

联系我们:uytrv@hotmail.com 问答工具