Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 2.23 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Priority of a priority queue always needs to be integral?
  2. struct SillyJob
  3. {
  4.     std::string description;
  5.     std::string priority;
  6.     // ...
  7. };
  8.        
  9. Silly: (by description length)
  10. LOW: very very long description
  11. HIGH: short
  12. ------------------------------------------------------------
  13. Not so silly: (by priority value)
  14. HIGH: short
  15. LOW: very very long description
  16.        
  17. #include <queue>
  18. #include <algorithm>
  19. #include <functional>
  20. #include <iostream>
  21. #include <string>
  22. #include <map>
  23.  
  24. struct SillyJob
  25. {
  26.     std::string description;
  27.     std::string priority;
  28.  
  29.     SillyJob(const std::string& d, const std::string& p)
  30.         : description(d), priority(p) { }
  31.  
  32.     bool operator<(const SillyJob& sj) const { return description.size() < sj.description.size(); }
  33.  
  34.     friend std::ostream& operator<<(std::ostream& os, const SillyJob& sj)
  35.     { return os << sj.priority << ": " << sj.description; }
  36. };
  37.  
  38. static bool by_priority(const SillyJob& a, const SillyJob& b)
  39. {
  40.     static std::map<std::string, int> prio_map;
  41.     if (prio_map.empty())
  42.     {
  43.         prio_map["HIGH"]   = 3;
  44.         prio_map["MEDIUM"] = 2;
  45.         prio_map["LOW"]    = 1;
  46.     }
  47.  
  48.     return prio_map[a.priority] < prio_map[b.priority];
  49. }
  50.  
  51. int main()
  52. {
  53.     std::cout << "Silly: (by description length)" << std::endl;
  54.     {
  55.         // by description length (member operator<)
  56.         std::priority_queue<SillyJob> silly_queue;
  57.  
  58.         silly_queue.push(SillyJob("short", "HIGH"));
  59.         silly_queue.push(SillyJob("very very long description", "LOW"));
  60.  
  61.         while (!silly_queue.empty())
  62.         {
  63.             std::cout << silly_queue.top() << std::endl;
  64.             silly_queue.pop();
  65.         }
  66.     }
  67.  
  68.     std::cout << std::string(60, '-') << "nNot so silly: (by priority value)" << std::endl;
  69.     {
  70.         // by description length (member operator<)
  71.         typedef bool (*cmpf)(const SillyJob&, const SillyJob&);
  72.         typedef std::priority_queue<SillyJob, std::vector<SillyJob>, cmpf> not_so_silly_queue;
  73.  
  74.         not_so_silly_queue queue(by_priority);
  75.  
  76.         queue.push(SillyJob("short", "HIGH"));
  77.         queue.push(SillyJob("very very long description", "LOW"));
  78.  
  79.         while (!queue.empty())
  80.         {
  81.             std::cout << queue.top() << std::endl;
  82.             queue.pop();
  83.         }
  84.     }
  85.  
  86. }