fork download
  1. //#include "stdafx.h"
  2. #include <iostream>
  3. #include <stack>
  4. #include <queue>
  5. #include <typeinfo>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9.  
  10. template <class T, class S, class C> S& Container(priority_queue<T, S, C>& pq)
  11. {
  12. struct hpQueue : private priority_queue<T, S, C> {static S& Container(priority_queue<T, S, C>& pq){return pq.*&hpQueue::c;}};
  13. return hpQueue::Container(pq);
  14. }
  15.  
  16. template <class T, class S> S& Container(stack<T, S>& s)
  17. {
  18. struct hStack : private stack<T, S>{static S& Container(stack<T, S>& s){return s.*&hStack::c;}};
  19. return hStack::Container(s);
  20. }
  21.  
  22. template <class T, class S> S& Container(queue<T, S>& q)
  23. {
  24. struct hQueue : private queue<T, S> {static S& Container(queue<T, S>& q){return q.*&hQueue::c;}};
  25. return hQueue::Container(q);
  26. }
  27.  
  28. template<class ContainerAdapter>
  29. void print_sqp(ContainerAdapter& cad)
  30. {
  31. bool isStack = (strstr(typeid(cad).name(), "class std::stack") != NULL);
  32. while (!cad.empty())
  33. {
  34. auto container = Container(cad);
  35. auto value = isStack ? container.back() : container.front();
  36. cout << value << " ";
  37. cad.pop();
  38. }
  39. cout << endl;
  40. }
  41.  
  42. int main()
  43. {
  44. std::stack<int> foo;
  45. std::queue<int> bar;
  46. std::priority_queue<int> baz;
  47.  
  48. for(int n : {1,8,5,6,3,4,0,9,7,2})
  49. {
  50. foo.push(n);
  51. bar.push(n);
  52. baz.push(n);
  53. }
  54. print_sqp(foo);
  55. print_sqp(bar);
  56. print_sqp(baz);
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1 1 1 1 1 1 1 1 1 1 
1 8 5 6 3 4 0 9 7 2 
9 8 7 6 5 4 3 2 1 0