fork download
  1. #include <iostream>
  2. #include <deque>
  3. using namespace std;
  4.  
  5. template<
  6. typename T,
  7. class Container = deque<T>
  8. > class stack{
  9. public:
  10. using value_type = T;
  11. using container_type = Container;
  12. using reference = typename container_type::reference;
  13. using const_reference = typename container_type::const_reference;
  14. using size_type = ::size_t;
  15.  
  16. stack(const container_type &container_): container(container_){}
  17. stack(container_type &&container_ = container_type()): container(container_){}
  18.  
  19. bool empty() const{
  20. return container.empty();
  21. }
  22.  
  23. size_type size() const{
  24. return container.size();
  25. }
  26.  
  27. reference &top(){
  28. return container.back();
  29. }
  30.  
  31. const reference &top() const{
  32. return container.back();
  33. }
  34.  
  35. void push(const value_type &val){
  36. container.push_back(val);
  37. }
  38.  
  39. void push(value_type &&val){
  40. container.push_back(val);
  41. }
  42.  
  43. template <class... Args>
  44. void emplace(Args &&...args){
  45. container.emplace_back(args...);
  46. }
  47.  
  48. void pop(){
  49. container.pop_back();
  50. }
  51. private:
  52. container_type container;
  53. };
  54.  
  55. template<typename T>
  56. struct push_many_proxy{
  57. using pushable_type = T;
  58. using value_type = typename pushable_type::value_type;
  59. pushable_type &pushable;
  60.  
  61. push_many_proxy(pushable_type &pushable_): pushable(pushable_){}
  62.  
  63. push_many_proxy &operator()(const value_type &val){
  64. pushable.push(val);
  65. return *this;
  66. }
  67.  
  68. push_many_proxy &operator()(value_type &&val){
  69. pushable.push(val);
  70. return *this;
  71. }
  72. };
  73.  
  74. template<typename T>
  75. push_many_proxy<T> push_many(T &pushable){
  76. return push_many_proxy<T>(pushable);
  77. }
  78.  
  79.  
  80. int main(){
  81. using nums_stack = stack<int>;
  82. nums_stack nums;
  83. push_many(nums)(1)(2)(3)(4)(5);
  84.  
  85. while(!nums.empty()){
  86. cout << nums.top();
  87. nums.pop();
  88. }
  89. return 0;
  90. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
54321