resip學習筆記之Timer

在resip中Timer的實現方式是將“定時時長+系統現在時間”插入一個priority_queue中,然後再從priority_queue依此彈出已到時的timer對象進行相關聯的任務處理,priority_queue是標準模板庫中的一個結構,它的作用是按指定的比較方法按需排列隊列中的元素,priority_queue的使用參見如下代碼。

#include<queue>
#include <functional>
#include<iostream>

using namespace std;

int main()
{
    priority_queue<int, vector<int>, greater<int>> myQueue;
    myQueue.push(1);
    myQueue.push(10);
    myQueue.push(5);
    myQueue.push(5);
    myQueue.push(8);
    myQueue.push(2);

    while (!myQueue.empty())
    {
        int n = myQueue.top();
        myQueue.pop();
        cout << n << endl;
    }
    return 0;
}

運行結果:

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