• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    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
    • 閱讀 ( 973 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    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 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆