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