Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printing elements of stack

774 views
Skip to first unread message

arnuld

unread,
Sep 17, 2014, 1:38:34 AM9/17/14
to
WANT: print stack elements
PROBLEM: No way to print

This is the code from section 12.1.2 of Nicolai's 2nd edition:

#include <iostream>
#include <stack>

int main()
{
std::stack<int> st;

st.push(10);
st.push(11);
st.push(12);

std::cout << ":STACK:" << std::endl;
std::cout << "size = " << st.size() << std::endl;

while(!st.empty())
{
std::cout << st.top() << "\n";
st.pop();
}

std::cout << std::endl;


return 0;
}



It prints the elements but makes empty too. Is there no way to print
stack elements without pop()-ing them out ?



Christian Gollwitzer

unread,
Sep 17, 2014, 2:01:40 AM9/17/14
to
Am 17.09.14 07:38, schrieb arnuld:
> WANT: print stack elements
> PROBLEM: No way to print

Stacks do not provide the facility to iterate over the elements. But you
can use a std::vector instead of a stack easily:

> This is the code from section 12.1.2 of Nicolai's 2nd edition:

Adapted:

#include <iostream>
#include <vector>

int main()
{
std::vector<int> st;

st.push_back(10);
st.push_back(11);
st.push_back(12);

std::cout << ":STACK:" << std::endl;
std::cout << "size = " << st.size() << std::endl;

std::cout<<"Stack content: "<<std::endl;
// print non-destructively
for (auto &x: st) {
std::cout<<x<<std::endl;
}

std::cout<<"Popping down stack:"<<std::endl;
// now popdown stack
while(!st.empty())
{
std::cout << st.back() << "\n";
st.pop_back();
}

std::cout << std::endl;


return 0;
}

Christian

Jorgen Grahn

unread,
Sep 17, 2014, 6:34:58 AM9/17/14
to
On Wed, 2014-09-17, Christian Gollwitzer wrote:
> Am 17.09.14 07:38, schrieb arnuld:
>> WANT: print stack elements
>> PROBLEM: No way to print
>
> Stacks do not provide the facility to iterate over the elements. But you
> can use a std::vector instead of a stack easily:
>
>> This is the code from section 12.1.2 of Nicolai's 2nd edition:
>
> Adapted:
>
> #include <iostream>
> #include <vector>
>
> int main()
> {
> std::vector<int> st;

That's not a stack in the OP's sense -- but perhaps that was your
point?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Victor Bazarov

unread,
Sep 17, 2014, 7:53:05 AM9/17/14
to
Stack is not a container over which you can iterate without altering it.
In that sense it's similar to streams.

V
--
I do not respond to top-posted replies, please don't ask

Charles J. Daniels

unread,
Sep 17, 2014, 4:16:31 PM9/17/14
to
You could pop a stack and push the elements onto a new stack after printing them. They'd be "upside-down" but when you're done you could pop them off back onto the original stack.

Rick C. Hodgin

unread,
Sep 17, 2014, 4:23:37 PM9/17/14
to
On Wednesday, September 17, 2014 1:38:34 AM UTC-4, arnuld wrote:
> WANT: print stack elements
> PROBLEM: No way to print
>
> Is there no way to print stack elements without pop()-ing them out ?

The stack stores memory somewhere. Generate the assembly and determine
how it's being accessed through the standard C++ mechanisms. You can
then iterate through the same memory manually using a pointer of some
kind.

In the alternative, create your own stack system using Christian's
suggestion.

Best regards,
Rick C. Hodgin

Scott Neugroschl

unread,
Sep 17, 2014, 5:54:53 PM9/17/14
to
Alternatively, derive from std::stack<> using private inheritance.
The underlying container in std::stack is protected.


Rosario193

unread,
Sep 19, 2014, 12:48:21 AM9/19/14
to
On 17 Sep 2014 05:38:11 GMT, arnuld <sun...@invalid.address> wrote:

>WANT: print stack elements
>PROBLEM: No way to print

it is possible to print using onhother stack...

#include <iostream>
#include <stack>

int main(void)
{ std::stack<int> st, v;
int r;

st.push(10);
st.push(11);
st.push(12);

std::cout << ":STACK:" << std::endl;
std::cout << "size = " << st.size() << std::endl;

while(!st.empty())
{//r=st.pop();
r=st.top(); st.pop();
std::cout << r << "\n";
v.push(r);
}
while(!v.empty())
{//r=v.pop();
r=v.top(); v.pop();
st.push(r);

Christian Gollwitzer

unread,
Sep 19, 2014, 1:11:40 AM9/19/14
to
Am 17.09.14 12:34, schrieb Jorgen Grahn:
> On Wed, 2014-09-17, Christian Gollwitzer wrote:
>> Stacks do not provide the facility to iterate over the elements. But you
>> can use a std::vector instead of a stack easily:
>>
>> std::vector<int> st;
>
> That's not a stack in the OP's sense -- but perhaps that was your
> point?

Yes, it was, you even quoted my statement saying so:)

Christian

Jorgen Grahn

unread,
Sep 19, 2014, 1:14:17 PM9/19/14
to
Oops. Indeed I did :-)
0 new messages