← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 13 of 15

C++ का stack STL

एक stack plates के एक pile जैसा है: आप top में add करते हैं और top से लेते हैं, इसलिए last वाला in first वाला out है। यह undo और brackets match करने के लिए useful है।
Syntax
cpp
#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?

cpp
#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;
}

push(), pop() और top()

push() top पर एक element add करता है। top() इसे remove किए बिना top element का एक reference return करता है। pop() top element remove करता है लेकिन इसकी value return नहीं करता -- अगर आपको ज़रूरत हो तो पहले इसे top() से पढ़ें।

उदाहरण: push(), pop() and top()

cpp
#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;
}

LIFO Behavior

एक stack की defining property यह है कि सबसे हाल में pushed element हमेशा popped होने वाला पहला होता है -- Last In, First Out। यह undo operations, function call tracking, और expression parsing के लिए ideal बनाता है।

उदाहरण: LIFO Behavior

cpp
#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;
}

empty() और size() Check करना

empty() true return करता है अगर stack में कोई elements न हों। size() current element count return करता है। undefined behavior से बचने के लिए top() या pop() call करने से पहले हमेशा empty() check करें।

उदाहरण: Checking empty() and size()

cpp
#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;
}

Practical Use: Balanced Parentheses

एक classic stack use case यह check करना है कि एक expression में brackets balanced हैं या नहीं -- opening brackets push करें, और एक closing bracket दिखने पर pop करें और compare करें।

उदाहरण: Practical Use: Balanced Parentheses

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक empty stack पर top() या pop() call करना, जो undefined behavior है।
  2. यह उम्मीद करना कि pop() removed value return करेगा, जब यह void return करता है और आपको पहले top() call करना चाहिए।
  3. एक stack पर loop करने की कोशिश करना, जो कोई iterators offer नहीं करता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.