1. 程式人生 > >STL中棧和佇列的使用方法

STL中棧和佇列的使用方法

/**//*
*===================================*
|                                   |
|       STL中優先佇列使用方法       |
|                                   |        
|       chenlie                     |
|                                   |
|       2010-3-24                   |
|                                   |
*===================================*
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int c[100];

struct cmp1
{
     bool operator ()(int x, int y)
    {
        return x > y;//小的優先順序高
    }
};

struct cmp2
{
    bool operator ()(const int x, const int y)
    {
        return c[x] > c[y]; 
       // c[x]小的優先順序高,由於可以在對外改變隊內的值,
        //所以使用此方法達不到真正的優先。建議用結構體型別。
    }
};

struct node
{
    int x, y;
    friend bool operator < (node a, node b)
    {
        return a.x > b.x;//結構體中,x小的優先順序高
    }
};


priority_queue<int>q1;

priority_queue<int, vector<int>, cmp1>q2;

priority_queue<int, vector<int>, cmp2>q3;

priority_queue<node>q4;


queue<int>qq1;
queue<node>qq2;

int main()
{
    int i, j, k, m, n;
    int x, y;
    node a;
    while (cin >> n)
    {
        for (i = 0; i < n; i++)
        {
            cin >> a.y >> a.x;
            q4.push(a);
        }
        cout << endl;
        while (!q4.empty())
        {
            cout << q4.top().y << " " << q4.top().x << endl;
            q4.pop();
        }
    //    cout << endl;
    }
    return 0;
}

轉載於:http://www.cppblog.com/CodeStream/archive/2011/03/25/142700.html