STL容器使用DEMO-priority_queue

来源:互联网 发布:淘宝首页是静态页面吗 编辑:程序博客网 时间:2024/06/07 03:27
Code:
  1. //////////////////////////////////////////////////////////////////////////   
  2. //  CopyRight(c) 2009, YOYO, All Rights Reserved.   
  3. //  Author: LIN YiQian   
  4. //  Created: 2009/08/24   
  5. //  Describe: STL priority_queue 使用DEMO   
  6. //////////////////////////////////////////////////////////////////////////   
  7. #include <iostream>   
  8. #include <queue>   
  9. #include <vector>   
  10. #include <Windows.h>   
  11.   
  12. using namespace std;   
  13.   
  14. //  队列元素   
  15. template <class T>   
  16. class PriorityMessage   
  17. {   
  18. public:   
  19.     PriorityMessage(int priority, T objMessage): m_nPriority(priority), m_objMessage(objMessage)   
  20.     {   
  21.         LARGE_INTEGER liValue;   
  22.         ::QueryPerformanceCounter(&liValue);   
  23.         m_tInQueue = liValue.LowPart;   
  24.     }   
  25.     ~PriorityMessage()   
  26.     {   
  27.     }   
  28.   
  29. public:   
  30.     int m_nPriority;   
  31.     unsigned long m_tInQueue;   
  32.     T m_objMessage;   
  33. };   
  34.   
  35. //  比较器   
  36. template <class _Ty>   
  37. struct PMessageCmp: public binary_function<_Ty, _Ty, bool>   
  38. {   
  39.     bool operator()(const PriorityMessage<_Ty>& _Left, const PriorityMessage<_Ty>& _Right) const  
  40.     {   
  41.         if (_Left.m_nPriority != _Right.m_nPriority)   
  42.         {   
  43.             return _Left.m_nPriority < _Right.m_nPriority;   
  44.         }   
  45.         else  
  46.         {   
  47.             return _Left.m_tInQueue < _Right.m_tInQueue;   
  48.         }   
  49.     }   
  50. };   
  51.   
  52. typedef PriorityMessage<string> STR_PM;   
  53. typedef priority_queue<STR_PM, vector<STR_PM>, PMessageCmp<string> > STR_PMQ;   
  54.   
  55. void main(void)   
  56. {   
  57.     STR_PMQ pmqStr;   
  58.   
  59.     //  插入优先队列   
  60.     pmqStr.push(STR_PM(1, "level11"));   
  61.     pmqStr.push(STR_PM(3, "level31"));   
  62.     pmqStr.push(STR_PM(1, "level12"));   
  63.     pmqStr.push(STR_PM(2, "level21"));   
  64.     pmqStr.push(STR_PM(3, "level32"));   
  65.     pmqStr.push(STR_PM(4, "level41"));   
  66.     pmqStr.push(STR_PM(5, "level51"));   
  67.     pmqStr.push(STR_PM(2, "level23"));   
  68.     pmqStr.push(STR_PM(4, "level42"));   
  69.     pmqStr.push(STR_PM(1, "level13"));   
  70.     pmqStr.push(STR_PM(2, "level24"));   
  71.     pmqStr.push(STR_PM(4, "level43"));   
  72.     pmqStr.push(STR_PM(4, "level44"));   
  73.   
  74.     //  打印优先队列   
  75.     while (!pmqStr.empty())   
  76.     {   
  77.         STR_PM& pmStr = pmqStr.top();   //  获取头部元素   
  78.         cout << "[LEVEL: " << pmStr.m_nPriority << "] "  
  79.             << "[QUEUE TIME: " << pmStr.m_tInQueue << "] "  
  80.              << "[MESSAGE: " << pmStr.m_objMessage.c_str() << "] " << endl;   
  81.         pmqStr.pop();   
  82.     }   
  83.   
  84.     system("pause");   
  85. }  

 

原创粉丝点击