How to print the contents of std :: stack and return its size?

In C ++, how can I print the contents of my stack and return its size?

std::stack<int> values; values.push(1); values.push(2); values.push(3); // How do I print the stack? 
+7
source share
5 answers

You can make a copy of the stack and pop elements one by one to reset them:

 #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; } 

Exit

 19 7 3 1 (4 elements) 

See here: http://liveworkspace.org/code/9489ee305e1f55ca18c0e5b6fa9b546f

+17
source

Both std::stack and std::queue are wrappers around a common container. This container is available as a member of protected c . Using c , you can get effective access to elements; otherwise, you can simply copy the stack or queue and destroy access to the elements of the copy.

An example of using c :

 #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 ); } 
+5
source

The only way to print std::stack elements without using them is to write an adapter that extends std::stack ( here's an example ). Otherwise, you should replace your stack with std::deque .

+4
source

http://www.cplusplus.com/reference/stl/stack/ for size it is easy to use:

 cout << mystack.size(); 

For the rest, I did not see anything in the document, but you should print the contents of your stack when you click it, or have a list with it in order to record an item only for printing, do not forget to delete it when you finish testing

0
source

Hmm, almost a 10 year question. Anyway, here is an additional answer.

Modern C ++ STL with algorithms is used more and more. So the following solution uses this. A prerequisite is that the stack uses continuous memory. This is guaranteed at the moment.

Conclusion is carried out through one liner.

See the following example:

 #include <vector> #include <stack> #include <iostream> #include <algorithm> #include <iterator> #include <sstream> using Number = int; using UnderlyingContainer = std::vector<Number>; using Stack = std::stack< Number, UnderlyingContainer>; std::istringstream testData("5 8 1 4 9 3"); int main() { // Put the test data onto the stack Stack stack{ UnderlyingContainer {std::istream_iterator<Number>(testData),std::istream_iterator<Number>()} }; // Print the test data if (not stack.empty()) std::copy(&stack.top() + 1 - stack.size(), &stack.top() + 1, std::ostream_iterator<Number>(std::cout, "\n")); return 0; } 

This is fully valid and reliable code. Here are some more explanations.

We want to output the data, so copy it to ostream_iterator. Ostream_iterator accepts a stream reference (yes, you can also specify an open stream) and a separator. Maybe you want to use "".

The source for the copy is 2 iterators. And yes, pointers are iterators. And we use guaranteed continuous memory for std :: stack. So we just compute 2 pointers and pass them to std :: copy.

And if you want to use explicit iterators. Like this. ,

 #include <vector> #include <stack> #include <iostream> #include <algorithm> #include <iterator> #include <sstream> using Number = int; using UnderlyingContainer = std::vector<Number>; using Stack = std::stack< Number, UnderlyingContainer>; using StackIterator = const Number *; std::istringstream testData("5 8 1 4 9 3"); int main() { // Put the test data onto the stack Stack stack{ UnderlyingContainer {std::istream_iterator<Number>(testData),std::istream_iterator<Number>()} }; // Print the test data // Get iterators StackIterator end = &stack.top() + 1; StackIterator begin = end - stack.size(); if (not stack.empty()) std::copy(begin, end, std::ostream_iterator<Number>(std::cout, "\n")); return 0; } 

This way you can create iterators for the stack. But, be careful:

Std :: stack intentionally hides its elements under the hood. So if you get access to the data for recording, I would consider this a design error. Read access through constant pointers / iterators is ok for me. But maybe you better use std :: vector. ,,

0
source

All Articles