×

# Stack memory issue

 0 I was solving a question which required (according to my solution) stack implementation. First I used STL stack for the it. But memory limit was exceeded. Then I created my own class for stack but issue was still the same, which at least cleared that STL was not at fault. I need your advice, why is occupying 65952 KiB (that too at error). Please note: I have provided code so that there is no other confusion as my code could be wrong as well. I am asking anyone to debug it, I am just interested in knowing memory issues. And code is correctly working, those ampersand after cin or cout are actually appropriate >> and << operators, please ignore them Code is provided below #include using namespace std; class st{ vector arr; int tp; public: st(){ tp = -1; } void push(int a){ ++tp; arr.push_back(a); } void pop(){ --tp; } int top(){ return arr[tp]; } int size(){ return tp+1; } void reset(){ tp = -1; } }; int main(){ int t; cin >> t; st s; while(t--){ s.reset(); vector vec; vector del; vector::iterator itr; int a,n,k; cin >> n >> k; for(int i = 0; i < n; ++i){ cin >> a; vec.push_back(a); if(i > 0){ while(s.size() and a > vec[s.top()]){del.push_back(s.top());s.pop();} } s.push(i); } int i; for(i = 0; i < k && i < del.size(); ++i){ itr = vec.begin()+del[i]-i; vec.erase(itr); } if(i != k){ while(i < k){ itr = vec.end()-i; vec.erase(itr); ++i; } } for(i = 0; i < vec.size(); ++i) cout << vec[i] << " "; cout << endl; } return 0 ; }  asked 04 Jun '18, 06:25 4★ay2306 232●9 accept rate: 11% first of all post the question for which u implemented this code. (04 Jun '18, 15:04) https://www.hackerearth.com/practice/data-structures/linked-list/singly-linked-list/practice-problems/algorithm/remove-friends-5/ (05 Jun '18, 18:24) ay23064★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,917
×173
×20

question asked: 04 Jun '18, 06:25

question was seen: 91 times

last updated: 05 Jun '18, 18:24