自定义单调队列模板.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <deque>
  2. #include <vector>
  3. class monque_inc{
  4. //compare function:from head to rear
  5. template<typename _T, typename _U>
  6. static std::vector<_T> ins(_T x, std::deque<_T>&que, bool (*comp)(_U, _U)) {
  7. std::vector<_T> vec;
  8. while (!((*comp)(que.back(), x)))
  9. vec.push_back(que.back()),
  10. que.pop_back();
  11. que.push_back(x);
  12. return vec;
  13. }
  14. //compare function:from head to rear
  15. template<typename _T, typename _U>
  16. static void ins(_T x, std::deque<_T>& que, bool (*comp)(_U, _U), std::vector<_T>& vec){
  17. while (!((*comp)(que.back(), x)))
  18. vec.push_back(que.back()),
  19. que.pop_back();
  20. que.push_back(x);
  21. }
  22. template<typename _T>
  23. static std::vector<_T> fiton(int siz, std::deque<_T>& que) {
  24. std::vector<_T> vec;
  25. while (siz > que.size())
  26. vec.push_back(que.front()),
  27. que.pop_front();
  28. return vec;
  29. }
  30. template<typename _T>
  31. static void fiton(int siz, std::deque<_T>& que, std::vector<_T>& vec) {
  32. while (siz > que.size())
  33. vec.push_back(que.front()),
  34. que.pop_front();
  35. }
  36. };