C++ का stack STL
In this page:
#include <stack>
std::stack<data_type> stack_name;
stack_name.push(value);
stack_name.top();
stack_name.pop();
std::stack क्या है?
std::stack एक container adapter है जो Last-In-First-Out (LIFO) access देता है। यह एक underlying container (default से deque) को wrap करता है और access restrict करता है ताकि elements सिर्फ top से add या remove हो सकें।
उदाहरण: What is std::stack?
#include <iostream>
#include <stack>
int main() {
std::stack<int> s; // LIFO container adapter
s.push(1);
s.push(2);
std::cout << s.top() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
push(), pop() और top()
push() top पर एक element add करता है। top() इसे remove किए बिना top element का एक reference return करता है। pop() top element remove करता है लेकिन इसकी value return नहीं करता -- अगर आपको ज़रूरत हो तो पहले इसे top() से पढ़ें।
उदाहरण: push(), pop() and top()
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
s.push(10);
std::cout << s.top() << std::endl; // returns without removing
s.pop(); // removes without returning
std::cout << s.size() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
LIFO Behavior
एक stack की defining property यह है कि सबसे हाल में pushed element हमेशा popped होने वाला पहला होता है -- Last In, First Out। यह undo operations, function call tracking, और expression parsing के लिए ideal बनाता है।
उदाहरण: LIFO Behavior
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
s.push(1);
s.push(2);
s.push(3);
std::cout << s.top() << std::endl; // most recently pushed comes out first
return 0;
}
Login to try C/C++/Java/PHP code in the editor
empty() और size() Check करना
empty() true return करता है अगर stack में कोई elements न हों। size() current element count return करता है। undefined behavior से बचने के लिए top() या pop() call करने से पहले हमेशा empty() check करें।
उदाहरण: Checking empty() and size()
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
if (s.empty()) { // check before calling top()/pop() on an empty stack
std::cout << "Empty" << std::endl;
}
std::cout << s.size() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Practical Use: Balanced Parentheses
एक classic stack use case यह check करना है कि एक expression में brackets balanced हैं या नहीं -- opening brackets push करें, और एक closing bracket दिखने पर pop करें और compare करें।
उदाहरण: Practical Use: Balanced Parentheses
// Include std::cout and std::cin
#include <iostream>
#include <stack>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string expr = "(()())";
std::stack<char> s;
// Declare balanced and set it to true
bool balanced = true;
for (char c : expr) {
if (c == '(') s.push(c);
else if (!s.empty()) s.pop();
else balanced = false;
}
// Print to the console with cout
std::cout << (balanced && s.empty()) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक empty stack पर
top()याpop()call करना, जो undefined behavior है। - यह उम्मीद करना कि
pop()removed value return करेगा, जब यहvoidreturn करता है और आपको पहलेtop()call करना चाहिए। - एक stack पर loop करने की कोशिश करना, जो कोई iterators offer नहीं करता।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: