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

C++ का queue STL

एक queue एक shop पर एक line जैसी है: line में पहला व्यक्ति पहले serve होता है। आप back में add करते हैं और front से remove करते हैं।
Syntax
cpp
#include <queue>

std::queue<data_type> queue_name;
queue_name.push(value);
queue_name.front();
queue_name.back();
queue_name.pop();

std::queue क्या है?

std::queue एक container adapter है जो First-In-First-Out (FIFO) access देता है। यह एक underlying container (default से deque) को wrap करता है और access restrict करता है ताकि elements back में add हों और front से remove हों।

उदाहरण: What is std::queue?

cpp
#include <iostream>
#include <queue>

int main() {
	std::queue<int> q; // FIFO container adapter
	q.push(1);
	q.push(2);
	std::cout << q.front() << std::endl;
	return 0;
}

push(), pop(), front() और back()

push() back में एक element add करता है। front() और back() पहले और आखिरी elements के references return करते हैं। pop() front element remove करता है लेकिन कुछ return नहीं करता -- ज़रूरत पड़ने पर पहले इसे front() से पढ़ें।

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

cpp
#include <iostream>
#include <queue>

int main() {
	std::queue<int> q;
	q.push(10);
	q.push(20);
	std::cout << q.front() << " " << q.back() << std::endl;
	q.pop(); // removes from the front
	std::cout << q.front() << std::endl;
	return 0;
}

FIFO Behavior

एक queue की defining property यह है कि सबसे पहले pushed element हमेशा popped होने वाला पहला होता है -- First In, First Out। यह real-world queues model करता है, जैसे line में wait कर रहे लोग या process होने का wait कर रहे tasks।

उदाहरण: FIFO Behavior

cpp
#include <iostream>
#include <queue>

int main() {
	std::queue<int> q;
	q.push(1);
	q.push(2);
	q.push(3);
	std::cout << q.front() << std::endl; // the first one pushed comes out first
	return 0;
}

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

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

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

cpp
#include <iostream>
#include <queue>

int main() {
	std::queue<int> q;
	if (q.empty()) { // check before calling front()/pop()
		std::cout << "Empty" << std::endl;
	}
	std::cout << q.size() << std::endl;
	return 0;
}

Practical Use: Level-Order Traversal

Queues breadth-first traversal के लिए standard tool हैं, जैसे एक tree को level by level process करना या BFS order में एक graph explore करना।

उदाहरण: Practical Use: Level-Order Traversal

cpp
#include <iostream>
#include <queue>

int main() {
	std::queue<int> q; // simulates BFS: process level by level
	q.push(1);
	while (!q.empty()) {
		int node = q.front();
		q.pop();
		std::cout << node << " ";
	}
	std::cout << std::endl;
	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 queue पर front() या pop() call करना, जो undefined behavior है।
  2. यह उम्मीद करना कि pop() value return करेगा, जब यह कुछ return नहीं करता।
  3. एक queue पर iterate करने की कोशिश करना, जिसमें कोई iterators नहीं हैं।

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.