{"id":154,"date":"2026-05-21T07:50:54","date_gmt":"2026-05-21T07:50:54","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=154"},"modified":"2026-05-24T15:42:27","modified_gmt":"2026-05-24T15:42:27","slug":"lists","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=lists","title":{"rendered":"Lists"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Lists in C++ are part of the Standard Template Library (STL). A list is a sequence container that stores elements in a non-contiguous way using a doubly linked list structure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a List?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A list is a container that stores elements where each element is linked to the previous and next element. Unlike arrays or vectors, lists do not use continuous memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Lists?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Lists are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allow fast insertion and deletion<\/li>\n\n\n\n<li>Do not require shifting elements<\/li>\n\n\n\n<li>Use dynamic memory efficiently<\/li>\n\n\n\n<li>Work well with frequent modifications<\/li>\n\n\n\n<li>Do not need continuous memory allocation<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">List Header File<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To use lists in C++, include the <code>&lt;list&gt;<\/code> library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;list&gt;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Declaring a List<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>list&lt;int&gt; numbers;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Initializing a List<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>list&lt;int&gt; numbers = {10, 20, 30};<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Adding Elements in List<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Lists allow insertion at both ends.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers.push_back(40);   \/\/ Add at end<br>numbers.push_front(5);   \/\/ Add at beginning<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Removing Elements from List<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers.pop_back();     \/\/ Remove last element<br>numbers.pop_front();    \/\/ Remove first element<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Example Program of List<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;list&gt;<br>using namespace std;<br><br>int main() {<br><br>    list&lt;int&gt; numbers = {1, 2, 3};<br><br>    numbers.push_front(0);<br>    numbers.push_back(4);<br><br>    for (int num : numbers) {<br><br>        cout &lt;&lt; num &lt;&lt; endl;<br>    }<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>0<br>1<br>2<br>3<br>4<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Accessing Elements in List<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Lists do not support direct indexing like vectors or arrays.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You must use iterators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>for (auto it = numbers.begin(); it != numbers.end(); it++) {<br><br>    cout &lt;&lt; *it &lt;&lt; endl;<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Common List 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_back()<\/code><\/td><td>Add element at end<\/td><\/tr><tr><td><code>push_front()<\/code><\/td><td>Add element at beginning<\/td><\/tr><tr><td><code>pop_back()<\/code><\/td><td>Remove last element<\/td><\/tr><tr><td><code>pop_front()<\/code><\/td><td>Remove first element<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Get number of elements<\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>Remove all elements<\/td><\/tr><tr><td><code>empty()<\/code><\/td><td>Check if list is empty<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Iterator Example with List<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;list&gt;<br>using namespace std;<br><br>int main() {<br><br>    list&lt;int&gt; numbers = {10, 20, 30};<br><br>    for (auto it = numbers.begin(); it != numbers.end(); it++) {<br><br>        cout &lt;&lt; *it &lt;&lt; endl;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Difference Between List and Vector<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Vector<\/th><th>List<\/th><\/tr><\/thead><tbody><tr><td>Contiguous memory<\/td><td>Non-contiguous memory<\/td><\/tr><tr><td>Fast random access<\/td><td>No random access<\/td><\/tr><tr><td>Slower insertion in middle<\/td><td>Fast insertion\/deletion<\/td><\/tr><tr><td>Better for indexing<\/td><td>Better for modifications<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Important Points About Lists<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Implemented as doubly linked list<\/li>\n\n\n\n<li>No direct indexing support<\/li>\n\n\n\n<li>Fast insertion and deletion<\/li>\n\n\n\n<li>Uses more memory than vector<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Lists<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Efficient insertion and deletion<\/li>\n\n\n\n<li>Dynamic memory usage<\/li>\n\n\n\n<li>No need for shifting elements<\/li>\n\n\n\n<li>Flexible structure<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes with Lists<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trying to use indexing (<code>numbers[0]<\/code>)<\/li>\n\n\n\n<li>Forgetting to use iterators<\/li>\n\n\n\n<li>Ignoring memory overhead<\/li>\n\n\n\n<li>Using list when vector is more suitable<\/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 chain of connected train compartments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each compartment is linked to next and previous<\/li>\n\n\n\n<li>You can easily add or remove compartments<\/li>\n\n\n\n<li>No fixed structure required<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is similar to how a list works.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Applications of Lists<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Lists are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Task scheduling systems<\/li>\n\n\n\n<li>Undo\/redo operations<\/li>\n\n\n\n<li>Memory management systems<\/li>\n\n\n\n<li>Real-time data processing<\/li>\n\n\n\n<li>Complex data structures<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Why Lists are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Lists are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Handle dynamic data efficiently<\/li>\n\n\n\n<li>Allow fast modifications<\/li>\n\n\n\n<li>Support advanced data structures<\/li>\n\n\n\n<li>Improve flexibility in programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Lists in C++ are powerful STL containers based on doubly linked lists. They are best suited for applications that require frequent insertion and deletion. Although they do not support direct indexing, they provide high flexibility and efficiency for dynamic data handling.<\/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) > Lists<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":53,"template":"","class_list":["post-154","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Lists - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn lists in C++ with STL functions, iterators, insertion, deletion, and linked list concepts for efficient dynamic data handling.\" \/>\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=\"Lists - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn lists in C++ with STL functions, iterators, insertion, deletion, and linked list concepts for efficient dynamic data handling.\" \/>\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:42:27+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=lists\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Lists - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:50:54+00:00\",\"dateModified\":\"2026-05-24T15:42:27+00:00\",\"description\":\"Learn lists in C++ with STL functions, iterators, insertion, deletion, and linked list concepts for efficient dynamic data handling.\",\"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) > Lists\"}]},{\"@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":"Lists - Learn C++Language with GiGz.PK","description":"Learn lists in C++ with STL functions, iterators, insertion, deletion, and linked list concepts for efficient dynamic data handling.","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":"Lists - Learn C++Language with GiGz.PK","og_description":"Learn lists in C++ with STL functions, iterators, insertion, deletion, and linked list concepts for efficient dynamic data handling.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:42:27+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=lists","url":"https:\/\/gigz.pk\/","name":"Lists - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:50:54+00:00","dateModified":"2026-05-24T15:42:27+00:00","description":"Learn lists in C++ with STL functions, iterators, insertion, deletion, and linked list concepts for efficient dynamic data handling.","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) > Lists"}]},{"@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\/154","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=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}