{"id":156,"date":"2026-05-21T07:52:10","date_gmt":"2026-05-21T07:52:10","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=156"},"modified":"2026-05-24T15:46:28","modified_gmt":"2026-05-24T15:46:28","slug":"queues","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=queues","title":{"rendered":"Queues"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Queues in C++ are part of the Standard Template Library (STL). A queue is a linear data structure that follows the FIFO (First In, First Out) principle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Queue?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A queue is a container where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first element added is the first one removed<\/li>\n\n\n\n<li>Elements are inserted from the back (rear)<\/li>\n\n\n\n<li>Elements are removed from the front<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Queues?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Queues are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Maintain order of processing<\/li>\n\n\n\n<li>Work like real-world waiting lines<\/li>\n\n\n\n<li>Help in task scheduling<\/li>\n\n\n\n<li>Are used in algorithms like BFS<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a ticket counter:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First person in line is served first<\/li>\n\n\n\n<li>New people join at the end<\/li>\n\n\n\n<li>No one skips the line<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is exactly how a queue works.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Queue Header File<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To use queues in C++, include the <code>&lt;queue&gt;<\/code> library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;queue&gt;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Declaring a Queue<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>queue&lt;int&gt; q;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Adding Elements (push)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Elements are added at the back of the queue.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>q.push(10);<br>q.push(20);<br>q.push(30);<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Removing Elements (pop)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Elements are removed from the front.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>q.pop();<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Accessing Elements<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">Front Element<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>cout &lt;&lt; q.front();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Back Element<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>cout &lt;&lt; q.back();<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Example Program of Queue<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;queue&gt;<br>using namespace std;<br><br>int main() {<br><br>    queue&lt;int&gt; q;<br><br>    q.push(1);<br>    q.push(2);<br>    q.push(3);<br><br>    cout &lt;&lt; \"Front: \" &lt;&lt; q.front() &lt;&lt; endl;<br>    cout &lt;&lt; \"Back: \" &lt;&lt; q.back() &lt;&lt; endl;<br><br>    q.pop();<br><br>    cout &lt;&lt; \"After pop, Front: \" &lt;&lt; q.front() &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Front: 1<br>Back: 3<br>After pop, Front: 2<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Common Queue Functions<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>push()<\/code><\/td><td>Add element at back<\/td><\/tr><tr><td><code>pop()<\/code><\/td><td>Remove front element<\/td><\/tr><tr><td><code>front()<\/code><\/td><td>Access first element<\/td><\/tr><tr><td><code>back()<\/code><\/td><td>Access last element<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Get number of elements<\/td><\/tr><tr><td><code>empty()<\/code><\/td><td>Check if queue is empty<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">How Queue Works<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Elements are inserted at the rear<\/li>\n\n\n\n<li>Elements wait in order<\/li>\n\n\n\n<li>First inserted is first removed<\/li>\n\n\n\n<li>Process continues sequentially<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">Types of Queues<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. Simple Queue<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Follows FIFO<\/li>\n\n\n\n<li>Basic queue structure<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Circular Queue<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Last position connects to first<\/li>\n\n\n\n<li>Efficient memory usage<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Priority Queue<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Elements are processed based on priority<\/li>\n\n\n\n<li>Not strictly FIFO<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Difference Between Stack and Queue<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Stack<\/th><th>Queue<\/th><\/tr><\/thead><tbody><tr><td>LIFO (Last In First Out)<\/td><td>FIFO (First In First Out)<\/td><\/tr><tr><td>Insert\/remove from top<\/td><td>Insert rear, remove front<\/td><\/tr><tr><td>Used in recursion<\/td><td>Used in scheduling<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Important Points About Queues<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Works on FIFO principle<\/li>\n\n\n\n<li>Insertion happens at rear<\/li>\n\n\n\n<li>Deletion happens at front<\/li>\n\n\n\n<li>No random access allowed<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Queues<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simple and efficient<\/li>\n\n\n\n<li>Maintains order<\/li>\n\n\n\n<li>Useful in scheduling systems<\/li>\n\n\n\n<li>Easy to implement using STL<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes with Queues<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trying to access middle elements<\/li>\n\n\n\n<li>Forgetting to check if queue is empty<\/li>\n\n\n\n<li>Calling <code>front()<\/code> on empty queue<\/li>\n\n\n\n<li>Confusing stack and queue behavior<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Applications<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Queues are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CPU scheduling<\/li>\n\n\n\n<li>Print queue systems<\/li>\n\n\n\n<li>Customer service systems<\/li>\n\n\n\n<li>Network packet handling<\/li>\n\n\n\n<li>BFS algorithm in graphs<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Why Queues are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Queues are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Manage tasks in order<\/li>\n\n\n\n<li>Improve system efficiency<\/li>\n\n\n\n<li>Help in real-time processing<\/li>\n\n\n\n<li>Are widely used in system design<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Queues in C++ are simple yet powerful STL containers that follow the FIFO principle. They are widely used in real-world applications like scheduling, processing tasks, and algorithm design where order matters.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional C++ > STL (Standard Template Library) > Queues<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":54,"template":"","class_list":["post-156","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Queues - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn queues in C++ with FIFO concepts, STL queue functions, examples, operations, and real-world applications in data structures.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Queues - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn queues in C++ with FIFO concepts, STL queue functions, examples, operations, and real-world applications in data structures.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-24T15:46:28+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=queues\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Queues - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:52:10+00:00\",\"dateModified\":\"2026-05-24T15:46:28+00:00\",\"description\":\"Learn queues in C++ with FIFO concepts, STL queue functions, examples, operations, and real-world applications in data structures.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional C++ > STL (Standard Template Library) > Queues\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Queues - Learn C++Language with GiGz.PK","description":"Learn queues in C++ with FIFO concepts, STL queue functions, examples, operations, and real-world applications in data structures.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Queues - Learn C++Language with GiGz.PK","og_description":"Learn queues in C++ with FIFO concepts, STL queue functions, examples, operations, and real-world applications in data structures.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:46:28+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=queues","url":"https:\/\/gigz.pk\/","name":"Queues - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:52:10+00:00","dateModified":"2026-05-24T15:46:28+00:00","description":"Learn queues in C++ with FIFO concepts, STL queue functions, examples, operations, and real-world applications in data structures.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Professional C++ > STL (Standard Template Library) > Queues"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}