I added(push) 4 items in a stack, right. Then, I wanted to remove all the items using pop function.
1 2 3 4 5 6 7 8 9 10 11 12 13
cout<<"\nDisplay all elements of stack"<<endl;
for(int i=0;i<trivial_stk.size();++i)
trivial_stk.peek(i); //outputs: "he" "hel" "hell" and "hello"
cout<<trivial_stk.size()<<endl; //output: 4
cout<<"\nPop elements off stack four times"<<endl;
trivial_stk.pop(); //output:"hello"
trivial_stk.pop();// output:"hell"
trivial_stk.pop();// output:"hel"
trivial_stk.pop(); //output: "he"
cout<<trivial_stk.size()<<endl; //output: 0
The above codes works fine
However, when I used for loop in implementing trivial_stk.pop();
It just goes until j=1 then loop stops.
1 2 3 4 5 6 7 8
cout<<trivial_stk.size()<<endl; //output: 4
cout<<"\nPop elements off stack four times"<<endl;
for(int j=0;j<trivial_stk.size();++j)
{trivial_stk.pop(); //outputs only "hello" and "hel"
}
cout<<trivial_stk.size()<<endl; //output: 2