C++ का queue STL
In this page:
#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?
#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;
}
Login to try C/C++/Java/PHP code in the editor
push(), pop(), front() और back()
push() back में एक element add करता है। front() और back() पहले और आखिरी elements के references return करते हैं। pop() front element remove करता है लेकिन कुछ return नहीं करता -- ज़रूरत पड़ने पर पहले इसे front() से पढ़ें।
उदाहरण: push(), pop(), front() and back()
#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;
}
Login to try C/C++/Java/PHP code in the editor
FIFO Behavior
एक queue की defining property यह है कि सबसे पहले pushed element हमेशा popped होने वाला पहला होता है -- First In, First Out। यह real-world queues model करता है, जैसे line में wait कर रहे लोग या process होने का wait कर रहे tasks।
उदाहरण: FIFO Behavior
#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;
}
Login to try C/C++/Java/PHP code in the editor
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()
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- एक empty queue पर
front()याpop()call करना, जो undefined behavior है। - यह उम्मीद करना कि
pop()value return करेगा, जब यह कुछ return नहीं करता। - एक queue पर iterate करने की कोशिश करना, जिसमें कोई iterators नहीं हैं।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: