C++STL之queue的使用

參考別人的哈,特此聲明。
/*
queue單向隊列與棧有點類似,一個是在同一端存取數據,另一個是在一端存入數據,另一端取出數據。
單向隊列中的數據是先進先出(First In First Out, FIFO)。在STL中,單向隊列也是以別的容器作爲底部結構,
再將接口改變,使之符合單向隊列的特性就可以了。因此實現也是非常方便的。
下面就給出單向隊列的函數列表和VS2008中單向隊列的源代碼。
單向隊列一共6個常用函數(front()、back()、push()、pop()、empty()、size()),與棧的常用函數較爲相似。
*/

//單向隊列 queue支持 empty() size() front() back() push() pop()
//By MoreWindows(http://blog.csdn.net/MoreWindows)
#include <queue>
#include <vector>
#include <list>
#include <cstdio>
using namespace std;

int main()
{
    //可以使用list作爲單向隊列的容器,默認是使用deque的。
    queue<int, list<int>> a;
    queue<int>        b;
    int i;

    //壓入數據
    for (i = 0; i < 10; i++)
    {
        a.push(i);
        b.push(i);
    }

    //單向隊列的大小
    printf("%d %d\n", a.size(), b.size());

    //隊列頭和隊列尾
    printf("%d %d\n", a.front(), a.back());
    printf("%d %d\n", b.front(), b.back());

    //取單向隊列項數據並將數據移出單向隊列
    while (!a.empty())
    {
        printf("%d ", a.front());
        a.pop();
    }
    putchar('\n');

    while (!b.empty())
    {
        printf("%d ", b.front());
        b.pop();
    }
    putchar('\n');

    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章