Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. std::stack<int> values;
  2. values.push(1);
  3. values.push(2);
  4. values.push(3);
  5.  
  6. // How do I print the stack?
  7.  
  8. #include <iostream>
  9. #include <stack>
  10. #include <string>
  11.  
  12. int main(int argc, const char *argv[])
  13. {
  14. std::stack<int> stack;
  15. stack.push(1);
  16. stack.push(3);
  17. stack.push(7);
  18. stack.push(19);
  19.  
  20. for (std::stack<int> dump = stack; !dump.empty(); dump.pop())
  21. std::cout << dump.top() << 'n';
  22.  
  23. std::cout << "(" << stack.size() << " elements)n";
  24.  
  25. return 0;
  26. }
  27.  
  28. 19
  29. 7
  30. 3
  31. 1
  32. (4) elements
  33.  
  34. cout << mystack.size();
  35.  
  36. #include <iostream> // std::wcout, std::endl
  37. #include <stack> // std::stack
  38. #include <stddef.h> // ptrdiff_t
  39. using namespace std;
  40.  
  41. typedef ptrdiff_t Size;
  42. typedef Size Index;
  43.  
  44. template< class Elem >
  45. Size nElements( stack< Elem > const& c )
  46. {
  47. return c.size();
  48. }
  49.  
  50. void display( stack<int> const& numbers )
  51. {
  52. struct Hack
  53. : public stack<int>
  54. {
  55. static int item( Index const i, stack<int> const& numbers )
  56. {
  57. return (numbers.*&Hack::c)[i];
  58. }
  59. };
  60.  
  61. wcout << numbers.size() << " numbers." << endl;
  62. for( Index i = 0; i < nElements( numbers ); ++i )
  63. {
  64. wcout << " " << Hack::item( i, numbers ) << endl;
  65. }
  66. }
  67.  
  68. int main()
  69. {
  70. stack<int> numbers;
  71. for( int i = 1; i <= 5; ++i ) { numbers.push( 100*i ); }
  72.  
  73. display( numbers );
  74. }
Add Comment
Please, Sign In to add comment