0

How can I add an element in the given stack?

I am trying to add 66 int the given stack {10,100,5,99,20} but I am hitting error in line 7. My code: #include<iostream> #include<stack> using namespace std; int main(){ stack<int>s; s={10, 100, 5, 99, 20}; for (int i=0;i<=5;i++){ s.push(66); } cout<<s.top(); return 0; }

10th Apr 2021, 1:51 AM
LIGHT
LIGHT - avatar
2 Antworten
+ 3
s={10, 100, 5, 99, 20}; This line isn't valid. stack doesn't work like this with an initializer list. You can use a loop to push the elements to the stack though; for (auto& num: {10, 100, 5, 99, 20}) { s.push(num); } Then you can just push 66 or whatever int as normal. s.push(66); https://code.sololearn.com/cl66OSlVXS16/?ref=app
10th Apr 2021, 2:14 AM
ChaoticDawg
ChaoticDawg - avatar
0
bro can you add me on facebook or telegram? I would need some help and guidance
10th Apr 2021, 3:57 AM
LIGHT
LIGHT - avatar