Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::stack<int> values;
- values.push(1);
- values.push(2);
- values.push(3);
- // How do I print the stack?
- #include <iostream>
- #include <stack>
- #include <string>
- int main(int argc, const char *argv[])
- {
- std::stack<int> stack;
- stack.push(1);
- stack.push(3);
- stack.push(7);
- stack.push(19);
- for (std::stack<int> dump = stack; !dump.empty(); dump.pop())
- std::cout << dump.top() << 'n';
- std::cout << "(" << stack.size() << " elements)n";
- return 0;
- }
- 19
- 7
- 3
- 1
- (4) elements
- cout << mystack.size();
- #include <iostream> // std::wcout, std::endl
- #include <stack> // std::stack
- #include <stddef.h> // ptrdiff_t
- using namespace std;
- typedef ptrdiff_t Size;
- typedef Size Index;
- template< class Elem >
- Size nElements( stack< Elem > const& c )
- {
- return c.size();
- }
- void display( stack<int> const& numbers )
- {
- struct Hack
- : public stack<int>
- {
- static int item( Index const i, stack<int> const& numbers )
- {
- return (numbers.*&Hack::c)[i];
- }
- };
- wcout << numbers.size() << " numbers." << endl;
- for( Index i = 0; i < nElements( numbers ); ++i )
- {
- wcout << " " << Hack::item( i, numbers ) << endl;
- }
- }
- int main()
- {
- stack<int> numbers;
- for( int i = 1; i <= 5; ++i ) { numbers.push( 100*i ); }
- display( numbers );
- }
Add Comment
Please, Sign In to add comment