{"id":1226,"date":"2023-09-06T12:06:57","date_gmt":"2023-09-06T12:06:57","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=1226"},"modified":"2023-10-02T06:42:54","modified_gmt":"2023-10-02T06:42:54","slug":"c-stdlist-from-basics-to-advanced","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/","title":{"rendered":"C++ std::list (&lt;list&gt;)- From Basics to Advanced"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is <code>std::list<\/code>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::list<\/code> is one of the sequence containers provided by the Standard Template Library (STL) in C++, that allows the storage and manipulation of a doubly-linked list of elements. Each element in the list is stored in a node, and each node contains a pointer to the previous node and the next node in the sequence. This structure grants the <code>std::list<\/code> certain advantages, as well as some limitations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Being a part of the STL, <code>std::list<\/code> offers a wide range of member functions that make it easy to insert, delete, and manage elements. One of the most noteworthy attributes of <code>std::list<\/code> is its ability to maintain the order of insertion, allowing for constant-time insertions and deletions from anywhere in the container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How does it compare to other C++ containers?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand where <code>std::list<\/code> stands in the realm of C++ containers, let&#8217;s compare it with a couple of other commonly used containers:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>std::vector<\/code>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Structure:<\/strong> Dynamic array.<\/li>\n\n\n\n<li><strong>Memory Allocation:<\/strong> Elements are stored contiguously in memory.<\/li>\n\n\n\n<li><strong>Insertion\/Deletion:<\/strong> Fast at the end but can be slow in the middle since elements might need to be shifted.<\/li>\n\n\n\n<li><strong>Access:<\/strong> Provides random access, meaning you can access any element directly through an index.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> When you need dynamic size and random access, and most insertions\/deletions are at the end.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>std::deque<\/code>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Structure:<\/strong> Double-ended queue.<\/li>\n\n\n\n<li><strong>Memory Allocation:<\/strong> Non-contiguous blocks of memory.<\/li>\n\n\n\n<li><strong>Insertion\/Deletion:<\/strong> Fast at both the beginning and the end.<\/li>\n\n\n\n<li><strong>Access:<\/strong> Provides random access, but slower compared to <code>std::vector<\/code>.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> When you need fast insertions\/deletions at both ends and the benefits of random access.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>std::list<\/code>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Structure:<\/strong> Doubly-linked list.<\/li>\n\n\n\n<li><strong>Memory Allocation:<\/strong> Non-contiguous. Each element is stored in a node with two pointers (next and previous).<\/li>\n\n\n\n<li><strong>Insertion\/Deletion:<\/strong> Fast anywhere in the list with a known iterator.<\/li>\n\n\n\n<li><strong>Access:<\/strong> Does not provide random access. You must traverse the list sequentially.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> When you need frequent insertions\/deletions in the middle, and the absence of random access is acceptable.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The choice between <code>std::list<\/code> and other containers largely depends on the specific requirements of your application. If you need efficient middle insertions and deletions and don&#8217;t require direct access to elements by index, <code>std::list<\/code> could be an ideal choice. Otherwise, containers like <code>std::vector<\/code> or <code>std::deque<\/code> might be more suitable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Concepts of <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As we set foot into the domain of C++ containers, it&#8217;s essential to grasp the foundational concepts that underpin <code>std::list<\/code>. This section offers a clear understanding of its characteristics, the underlying data structure, and how it contrasts with arrays and vectors in terms of advantages and limitations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Characteristics<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Size:<\/strong> Unlike arrays, <code>std::list<\/code> can adjust its size dynamically. As you insert or remove elements, it grows or shrinks accordingly.<\/li>\n\n\n\n<li><strong>Element Ordering:<\/strong> The order in which you insert elements is preserved. This ensures that the elements are always arranged in the sequence they were added.<\/li>\n\n\n\n<li><strong>Non-contiguous Memory:<\/strong> Unlike arrays or vectors, the elements in a <code>std::list<\/code> are not stored in a continuous block of memory. Each element is stored in its own node.<\/li>\n\n\n\n<li><strong>Bidirectional Iterators:<\/strong> <code>std::list<\/code> provides bidirectional iterators. This means you can iterate over the elements in both forward and reverse directions. However, it doesn\u2019t support random access iterators, so direct access via an index (like in arrays or vectors) isn&#8217;t possible.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Underlying Data Structure: Doubly-linked list<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The backbone of <code>std::list<\/code> is a doubly-linked list. Here&#8217;s a brief insight:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Nodes:<\/strong> Each element is stored in a node. A node comprises the data (element) and two pointers\u2014one pointing to the previous node and one to the next node.<\/li>\n\n\n\n<li><strong>Head and Tail:<\/strong> In a typical doubly-linked list, there&#8217;s a head (starting node) and a tail (ending node). The head&#8217;s previous pointer and the tail&#8217;s next pointer are generally null, indicating the start and end of the list, respectively.<\/li>\n\n\n\n<li><strong>Insertion and Deletion:<\/strong> Thanks to this structure, adding or removing nodes in between is relatively efficient. You just need to adjust the pointers of the neighboring nodes.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits and Drawbacks over Arrays\/Vectors<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Benefits:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Efficient Insertions\/Deletions:<\/strong> Inserting or deleting an element in the middle of a <code>std::list<\/code> is efficient, as it requires only the adjustment of a few pointers. In contrast, vectors or arrays might need to shift elements, making the operation more time-consuming.<\/li>\n\n\n\n<li><strong>No Memory Reallocation:<\/strong> Since <code>std::list<\/code> doesn&#8217;t use contiguous memory, adding elements doesn&#8217;t require reallocation of memory or moving other elements, as can be the case with vectors.<\/li>\n\n\n\n<li><strong>Fixed Size Overhead Per Element:<\/strong> Each element in a <code>std::list<\/code> has an overhead of two pointers (for next and previous). This overhead is consistent regardless of the list&#8217;s size.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Drawbacks:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Memory Usage:<\/strong> Due to the additional storage of two pointers for each element, <code>std::list<\/code> typically uses more memory than a vector or array storing the same data.<\/li>\n\n\n\n<li><strong>No Random Access:<\/strong> Direct access to an element via an index isn&#8217;t possible. To access the nth element, you&#8217;d need to traverse n nodes, making certain operations slower than with arrays or vectors.<\/li>\n\n\n\n<li><strong>Cache Unfriendliness:<\/strong> Since elements aren&#8217;t stored contiguously, cache locality is not as optimal as in vectors. This can lead to performance hits due to more frequent cache misses.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Started with <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have your environment set up and have familiarized yourself with the basic concepts, it&#8217;s time to dive into the practical implementation. The first and foremost step is to include the necessary header file to utilize <code>std::list<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to include the necessary header<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In C++, header files act as the gateway to various libraries and functionalities. To work with <code>std::list<\/code>, you need to include its specific header.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s how you do it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;list&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">By adding the above line at the beginning of your C++ program, you&#8217;re instructing the compiler to include the definitions and functionalities related to <code>std::list<\/code> provided by the Standard Template Library (STL).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To provide a simple illustration, here&#8217;s a short program that initializes a <code>std::list<\/code> with a few integer values and then prints them:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;list&gt;<\/span><\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-comment\">\/\/ Initialize a list with some values<\/span>\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\n\n    <span class=\"hljs-comment\">\/\/ Print the values<\/span>\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> num : numbers) {\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; num &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n    }\n\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">When you run this program, it will output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">1 2 3 4 5<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Now, with the header included and a basic understanding of initializing a <code>std::list<\/code>, you&#8217;re all set to explore its varied functionalities and operations in subsequent sections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating and Initializing <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The versatility of <code>std::list<\/code> is also mirrored in the variety of ways you can create and initialize it. Let&#8217;s explore some common methods to instantiate lists, providing you with a solid foundation to begin your foray into this useful container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Default constructor<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Using the default constructor, you can create an empty <code>std::list<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; emptyList;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here, <code>emptyList<\/code> is a list of integers that currently holds no values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialization with an array or other containers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you have data in other containers (like arrays or vectors), you can initialize a <code>std::list<\/code> using that data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Array:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">int<\/span> arr&#91;] = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\n<span class=\"hljs-function\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; <span class=\"hljs-title\">listFromArray<\/span><span class=\"hljs-params\">(arr, arr + <span class=\"hljs-keyword\">sizeof<\/span>(arr)\/<span class=\"hljs-keyword\">sizeof<\/span>(arr&#91;<span class=\"hljs-number\">0<\/span>]))<\/span><\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Vector:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">vector<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; vec = {<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>};\n<span class=\"hljs-function\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; <span class=\"hljs-title\">listFromVector<\/span><span class=\"hljs-params\">(vec.begin(), vec.end())<\/span><\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Both methods utilize the range constructor of <code>std::list<\/code>, which takes two iterators (beginning and end) of the container to copy the elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialization with a count and value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can also initialize a <code>std::list<\/code> by specifying the count of elements and the value that each of those elements should hold.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-function\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; <span class=\"hljs-title\">tenTwos<\/span><span class=\"hljs-params\">(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>)<\/span><\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here, <code>tenTwos<\/code> is a list that contains ten elements, all of which have the value <code>2<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Initialization Methods:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Initializer List:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Just as we&#8217;ve seen in previous examples, <code>std::list<\/code> can be initialized using an initializer list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">13<\/span>, <span class=\"hljs-number\">14<\/span>, <span class=\"hljs-number\">15<\/span>};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Copy Constructor:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can create a new list by copying an existing one.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; originalList = {<span class=\"hljs-number\">16<\/span>, <span class=\"hljs-number\">17<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">19<\/span>, <span class=\"hljs-number\">20<\/span>};\n<span class=\"hljs-function\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; <span class=\"hljs-title\">copiedList<\/span><span class=\"hljs-params\">(originalList)<\/span><\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Move Constructor (C++11 and above):<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you don&#8217;t need the data in the original list after copying, you can use the move constructor to transfer ownership without the overhead of copying.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-function\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; <span class=\"hljs-title\">movedList<\/span><span class=\"hljs-params\">(<span class=\"hljs-built_in\">std<\/span>::move(originalList))<\/span><\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Accessing Elements in <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike arrays or vectors, <code>std::list<\/code> does not provide random access to its elements due to its non-contiguous nature. However, there are still several ways to access its elements, and understanding these methods is crucial for effective utilization of the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>front()<\/code> and <code>back()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">These member functions provide direct access to the first and last elements of the list, respectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\n\n<span class=\"hljs-keyword\">int<\/span> firstElement = numbers.front(); <span class=\"hljs-comment\">\/\/ firstElement gets the value 1<\/span>\n<span class=\"hljs-keyword\">int<\/span> lastElement = numbers.back();  <span class=\"hljs-comment\">\/\/ lastElement gets the value 5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Iterators and their Importance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators are fundamental to <code>std::list<\/code> and many other STL containers. They act like pointers, pointing to elements within the container, and allow for sequential access to those elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why are Iterators Important?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Sequential Access:<\/strong> As mentioned, <code>std::list<\/code> doesn&#8217;t support direct indexing, so iterators provide a way to traverse and access elements sequentially.<\/li>\n\n\n\n<li><strong>Generic Algorithms:<\/strong> STL provides a set of algorithms (like <code>std::find<\/code>, <code>std::sort<\/code>, etc.) that work on different containers. These algorithms use iterators to work seamlessly across diverse containers.<\/li>\n\n\n\n<li><strong>Manipulation:<\/strong> Iterators are essential when you want to insert or erase elements at specific positions within a <code>std::list<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Basic Usage:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>};\n\n<span class=\"hljs-comment\">\/\/ Obtain iterators to the beginning and end of the list<\/span>\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt;::iterator beginIter = numbers.begin();\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt;::iterator endIter = numbers.end();\n\n<span class=\"hljs-comment\">\/\/ Use the iterators to traverse and print the list<\/span>\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt;::iterator it = beginIter; it != endIter; ++it) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *it &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The above code will output:Copy code<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">10 20 30 40 50<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Range-based For Loop (C++11 and above):<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">C++11 introduced range-based for loops, which provide a more concise way to iterate over containers. Here&#8217;s how you can use it with <code>std::list<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> num : numbers) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; num &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Reverse Iteration:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::list<\/code> provides bidirectional iterators, which means you can also traverse the list in reverse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt;::reverse_iterator rit = numbers.rbegin(); rit != numbers.rend(); ++rit) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *rit &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This will output:Copy code<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-number\">50<\/span> <span class=\"hljs-number\">40<\/span> <span class=\"hljs-number\">30<\/span> <span class=\"hljs-number\">20<\/span> <span class=\"hljs-number\">10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Manipulating <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding how to manipulate the elements of <code>std::list<\/code> is essential for effectively using the container. Below, we explore the various operations available to add, remove, and modify the elements of a <code>std::list<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inserting Elements<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>push_front()<\/code><\/strong>: Adds an element to the beginning of the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>};\nnumbers.push_front(<span class=\"hljs-number\">1<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2, 3, 4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong><code>push_back()<\/code><\/strong>: Appends an element to the end of the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.push_back(<span class=\"hljs-number\">5<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2, 3, 4, 5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong><code>insert()<\/code><\/strong>: Inserts one or multiple elements at a specific position in the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">auto<\/span> it = numbers.begin();\n<span class=\"hljs-built_in\">std<\/span>::advance(it, <span class=\"hljs-number\">2<\/span>);  <span class=\"hljs-comment\">\/\/ Move iterator to the third position<\/span>\nnumbers.insert(it, <span class=\"hljs-number\">6<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2, 6, 3, 4, 5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Removing Elements<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>pop_front()<\/code><\/strong>: Removes the first element from the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.pop_front();  <span class=\"hljs-comment\">\/\/ List becomes: 2, 6, 3, 4, 5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong><code>pop_back()<\/code><\/strong>: Removes the last element from the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.pop_back();  <span class=\"hljs-comment\">\/\/ List becomes: 2, 6, 3, 4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong><code>erase()<\/code><\/strong>: Deletes one or a range of elements from the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">it = numbers.begin();\n<span class=\"hljs-built_in\">std<\/span>::advance(it, <span class=\"hljs-number\">1<\/span>);\nnumbers.erase(it);  <span class=\"hljs-comment\">\/\/ List becomes: 2, 3, 4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong><code>remove()<\/code><\/strong>: Removes all elements with a specific value from the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.remove(<span class=\"hljs-number\">3<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 2, 4<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong><code>clear()<\/code><\/strong>: Removes all elements from the list, making it empty.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.clear();  <span class=\"hljs-comment\">\/\/ List is now empty<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Merging and Splitting Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Merging<\/strong>: You can merge two sorted lists using the <code>merge()<\/code> function. It combines two lists into one, placing all elements in sorted order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; list1 = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">5<\/span>};\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; list2 = {<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>};\nlist1.merge(list2);  <span class=\"hljs-comment\">\/\/ list1 becomes: 1, 2, 3, 4, 5, 6<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Splitting<\/strong>: While there&#8217;s no direct &#8220;split&#8221; function, you can use <code>splice()<\/code> to move elements from one list to another.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; listA = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>};\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; listB;\n<span class=\"hljs-keyword\">auto<\/span> splitPos = listA.begin();\n<span class=\"hljs-built_in\">std<\/span>::advance(splitPos, <span class=\"hljs-number\">3<\/span>);\nlistB.splice(listB.begin(), listA, listA.begin(), splitPos);  <span class=\"hljs-comment\">\/\/ listB becomes: 1, 2, 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Reversing and Sorting<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Reversing<\/strong>: To reverse the order of elements in a <code>std::list<\/code>, use the <code>reverse()<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; nums = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\nnums.reverse();  <span class=\"hljs-comment\">\/\/ nums becomes: 5, 4, 3, 2, 1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Sorting<\/strong>: Use the <code>sort()<\/code> function to arrange the elements in ascending (or custom) order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; unsorted = {<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">4<\/span>};\nunsorted.sort();  <span class=\"hljs-comment\">\/\/ unsorted becomes: 1, 3, 4, 5, 8<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Manipulating <code>std::list<\/code> is straightforward once you familiarize yourself with its set of functions. The key is to know which operation is optimal for a particular task, given the unique characteristics of this container.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Operations on <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you&#8217;ve grasped the basics, <code>std::list<\/code> offers a set of advanced operations that can greatly optimize and facilitate more intricate tasks. Here, we delve into some of these functions, shedding light on their utility and syntax.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>splice()<\/code><\/strong>: Transfer Elements from One List to Another<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>splice()<\/code> function transfers elements from one list to another without copying or reallocating them. This makes the operation highly efficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Transfer Entire List<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; listA = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>};\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; listB = {<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>};\nlistA.splice(listA.end(), listB);  <span class=\"hljs-comment\">\/\/ Moves all elements from listB to listA. listA: 1, 2, 3, 4, 5, 6; listB is now empty.<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Transfer Specific Element<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">auto<\/span> iter = listA.begin();\n<span class=\"hljs-built_in\">std<\/span>::advance(iter, <span class=\"hljs-number\">2<\/span>);  <span class=\"hljs-comment\">\/\/ Move iterator to the third element<\/span>\nlistB.splice(listB.begin(), listA, iter);  <span class=\"hljs-comment\">\/\/ Moves '3' from listA to the beginning of listB.<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Transfer Range of Elements<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">auto<\/span> start = listA.begin();\n<span class=\"hljs-keyword\">auto<\/span> end = listA.begin();\n<span class=\"hljs-built_in\">std<\/span>::advance(end, <span class=\"hljs-number\">2<\/span>);\nlistB.splice(listB.end(), listA, start, end);  <span class=\"hljs-comment\">\/\/ Moves the first two elements from listA to the end of listB.<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong><code>unique()<\/code><\/strong>: Remove Duplicate Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>unique()<\/code> function removes all but the first element from every consecutive group of equal elements in the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\nnumbers.unique();  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2, 3, 4, 5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You can also use <code>unique()<\/code> with a binary predicate to specify custom criteria for uniqueness.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>resize()<\/code><\/strong>: Change the Size of the List<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>resize()<\/code> function changes the size of the list, either by adding default-initialized elements or by truncating the list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Increase Size<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>};\nnumbers.resize(<span class=\"hljs-number\">5<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2, 3, 0, 0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Decrease Size<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.resize(<span class=\"hljs-number\">2<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Resize with Value<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.resize(<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">9<\/span>);  <span class=\"hljs-comment\">\/\/ List becomes: 1, 2, 9, 9<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong><code>swap()<\/code><\/strong>: Swap Two Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As the name suggests, <code>swap()<\/code> exchanges the contents of two lists. This operation is highly efficient and doesn&#8217;t involve actual element transfer.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-36\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; listA = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>};\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; listB = {<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>};\nlistA.swap(listB);  <span class=\"hljs-comment\">\/\/ listA: 4, 5, 6; listB: 1, 2, 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-36\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Iterating Over a <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Iterating over containers is a fundamental task in C++, and given the non-contiguous nature of <code>std::list<\/code>, it&#8217;s even more critical to understand the techniques available to navigate through its elements. Here, we&#8217;ll examine two primary methods: traditional iterators and the C++11 range-based for loops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Traditional Iterators<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators act much like pointers, pointing to elements within a container. <code>std::list<\/code> provides bidirectional iterators, which means you can traverse the list both forwards and backwards.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Forward Iteration<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-37\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>};\n\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt;::iterator it = numbers.begin(); it != numbers.end(); ++it) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *it &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}\n<span class=\"hljs-comment\">\/\/ Output: 10 20 30 40 50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-37\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Reverse Iteration<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bidirectional iterators allow for reverse traversal as well.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-38\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt;::reverse_iterator rit = numbers.rbegin(); rit != numbers.rend(); ++rit) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *rit &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}\n<span class=\"hljs-comment\">\/\/ Output: 50 40 30 20 10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-38\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Using C++11 Range-based For Loops<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The range-based for loop, introduced in C++11, provides a more concise and readable way to iterate over containers. This syntax abstracts away the intricacies of iterators, resulting in cleaner code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Standard Iteration<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-39\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> num : numbers) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; num &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}\n<span class=\"hljs-comment\">\/\/ Output: 10 20 30 40 50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-39\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Iterating with Auto<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using <code>auto<\/code> can make the loop even more concise, especially if dealing with complex data types. <code>auto<\/code> deduces the type of the variable from its initializer.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-40\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">auto<\/span> num : numbers) {\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; num &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-40\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Modification during Iteration<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you intend to modify the elements of the list during iteration, it&#8217;s recommended to use a reference.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-41\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">auto<\/span> &amp;num : numbers) {\n    num *= <span class=\"hljs-number\">2<\/span>;  <span class=\"hljs-comment\">\/\/ Double each number<\/span>\n}\n<span class=\"hljs-comment\">\/\/ numbers: 20, 40, 60, 80, 100<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-41\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Performance Considerations with <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When it comes to using STL containers, it&#8217;s crucial to understand their performance characteristics. <code>std::list<\/code> has its own set of advantages and caveats which can make a difference depending on the use-case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Time Complexities of Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some fundamental operations and their associated time complexities for <code>std::list<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Insertion or Deletion at the Front or Back<\/strong>: <em>O<\/em>(1)<\/li>\n\n\n\n<li><strong>Insertion or Deletion using an Iterator (known position)<\/strong>: <em>O<\/em>(1)<\/li>\n\n\n\n<li><strong>Accessing Elements (e.g., via index)<\/strong>: <em>O<\/em>(<em>n<\/em>) [Note: <code>std::list<\/code> doesn&#8217;t support random access like arrays\/vectors]<\/li>\n\n\n\n<li><strong>Searching for an Element<\/strong>: <em>O<\/em>(<em>n<\/em>)<\/li>\n\n\n\n<li><strong>Sorting<\/strong>: <em>O<\/em>(<em>n<\/em> log <em>n<\/em>)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use <code>std::list<\/code> vs. <code>std::vector<\/code> or Other Containers<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use <code>std::list<\/code> when<\/strong>:\n<ul class=\"wp-block-list\">\n<li>You have frequent insertions and deletions in the middle of the sequence. Since lists are implemented as doubly-linked lists, adding or removing an element in the middle takes constant time given an iterator to the position.<\/li>\n\n\n\n<li>Memory overhead isn&#8217;t a concern. Each node in a <code>std::list<\/code> has overhead for previous and next pointers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Prefer <code>std::vector<\/code> or <code>std::array<\/code> when<\/strong>:\n<ul class=\"wp-block-list\">\n<li>You need random access to elements. Vectors and arrays provide <em>O<\/em>(1) random access, whereas lists take <em>O<\/em>(<em>n<\/em>).<\/li>\n\n\n\n<li>Memory usage is a concern. Vectors and arrays typically use memory more efficiently due to their contiguous memory layout.<\/li>\n\n\n\n<li>Cache locality matters. The contiguous nature of vectors and arrays makes them more cache-friendly, often resulting in better performance for operations that process elements in sequence.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Real-world Benchmarks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In real-world scenarios, raw time complexities might not tell the whole story due to factors like cache locality, memory overhead, and compiler optimizations. Here&#8217;s a simplistic approach to benchmarking:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-42\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;list&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;vector&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;chrono&gt;<\/span><\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-keyword\">int<\/span> N = <span class=\"hljs-number\">1000000<\/span>;\n\n    <span class=\"hljs-comment\">\/\/ Benchmark for std::list<\/span>\n    {\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; lst;\n        <span class=\"hljs-keyword\">auto<\/span> start = <span class=\"hljs-built_in\">std<\/span>::chrono::high_resolution_clock::now();\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; N; ++i) {\n            lst.push_back(i);\n        }\n        <span class=\"hljs-keyword\">auto<\/span> end = <span class=\"hljs-built_in\">std<\/span>::chrono::high_resolution_clock::now();\n        <span class=\"hljs-built_in\">std<\/span>::chrono::duration&lt;<span class=\"hljs-keyword\">double<\/span>&gt; diff = end - start;\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"std::list time: \"<\/span> &lt;&lt; diff.count() &lt;&lt; <span class=\"hljs-string\">\" s\\n\"<\/span>;\n    }\n\n    <span class=\"hljs-comment\">\/\/ Benchmark for std::vector<\/span>\n    {\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">vector<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; vec;\n        <span class=\"hljs-keyword\">auto<\/span> start = <span class=\"hljs-built_in\">std<\/span>::chrono::high_resolution_clock::now();\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; N; ++i) {\n            vec.push_back(i);\n        }\n        <span class=\"hljs-keyword\">auto<\/span> end = <span class=\"hljs-built_in\">std<\/span>::chrono::high_resolution_clock::now();\n        <span class=\"hljs-built_in\">std<\/span>::chrono::duration&lt;<span class=\"hljs-keyword\">double<\/span>&gt; diff = end - start;\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"std::vector time: \"<\/span> &lt;&lt; diff.count() &lt;&lt; <span class=\"hljs-string\">\" s\\n\"<\/span>;\n    }\n\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-42\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This benchmark will give you an idea of the performance difference between <code>std::list<\/code> and <code>std::vector<\/code> for a simple <code>push_back<\/code> operation for a large number of elements. For a comprehensive benchmark, consider varying operations, sizes, and scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Pitfalls and Mistakes with <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using <code>std::list<\/code> or, in fact, any container from the C++ STL requires careful attention to avoid common pitfalls. Let&#8217;s explore some frequent mistakes developers make when using <code>std::list<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Invalid Iterator After Modification<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the most common pitfalls when working with containers is the potential invalidation of iterators, pointers, or references after certain modifying operations.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Safe with <code>std::list<\/code>:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inserting elements doesn&#8217;t invalidate any iterators.<\/li>\n\n\n\n<li>Erasing elements invalidates only the iterators (and references) to the erased elements.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mistake Example<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-43\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>};\n<span class=\"hljs-keyword\">auto<\/span> it = <span class=\"hljs-built_in\">std<\/span>::next(numbers.begin(), <span class=\"hljs-number\">2<\/span>);  <span class=\"hljs-comment\">\/\/ it points to '3'<\/span>\nnumbers.erase(numbers.begin());\n<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *it;  <span class=\"hljs-comment\">\/\/ This is safe; 'it' remains valid and will print '3'.<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-43\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">However, always be aware of the specific operation and its implications on iterator validity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Misusing methods like <code>splice()<\/code> or <code>remove()<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>splice()<\/code> and <code>remove()<\/code> functions are powerful but can introduce bugs if misused.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>splice()<\/code> Pitfall:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">One common mistake with <code>splice()<\/code> is attempting to splice a list into itself using invalid ranges, which results in undefined behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mistake Example<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-44\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>};\n<span class=\"hljs-keyword\">auto<\/span> start = <span class=\"hljs-built_in\">std<\/span>::next(numbers.begin());\n<span class=\"hljs-keyword\">auto<\/span> end = <span class=\"hljs-built_in\">std<\/span>::next(numbers.begin(), <span class=\"hljs-number\">3<\/span>);\nnumbers.splice(numbers.begin(), numbers, start, end);  <span class=\"hljs-comment\">\/\/ Undefined behavior: Invalid range within the same list<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-44\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\"><code>remove()<\/code> Pitfall:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>remove()<\/code> function removes all elements with a specific value. A common mistake is to believe it works with a predicate, which is actually the role of <code>remove_if()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mistake Example<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-45\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\nnumbers.remove(&#91;](<span class=\"hljs-keyword\">int<\/span> n) { <span class=\"hljs-keyword\">return<\/span> n % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>; });  <span class=\"hljs-comment\">\/\/ This is incorrect! remove() does not take a predicate.<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-45\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Corrected Code<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-46\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\">numbers.remove_if(&#91;](<span class=\"hljs-keyword\">int<\/span> n) { <span class=\"hljs-keyword\">return<\/span> n % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>; });  <span class=\"hljs-comment\">\/\/ This will correctly remove all even numbers<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-46\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">When working with <code>std::list<\/code>, always refer to the documentation or familiar sources to understand the intricacies of its operations. Avoid making assumptions, especially around iterator validity and function behavior, to ensure robust and error-free code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use Cases and Practical Examples with <code>std::list<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The linked list structure of <code>std::list<\/code> makes it particularly well-suited for tasks where elements are frequently inserted or removed from the middle of a collection. Let&#8217;s dive into a few practical use cases:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing a Simple Playlist<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine a music playlist where you can add songs, skip to the next song, or go back to the previous song.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-47\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Song<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    Song(<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">string<\/span> title) : title(title) {}\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">play<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Playing: \"<\/span> &lt;&lt; title &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">string<\/span> title;\n};\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Playlist<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">addSong<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">const<\/span> Song&amp; song)<\/span> <\/span>{\n        songs.push_back(song);\n        <span class=\"hljs-comment\">\/\/ If it's the first song, set it as the current<\/span>\n        <span class=\"hljs-keyword\">if<\/span> (songs.size() == <span class=\"hljs-number\">1<\/span>) {\n            current = songs.begin();\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">playCurrent<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (current != songs.end()) {\n            current-&gt;play();\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Playlist is empty or ended.\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">next<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (current != songs.end() &amp;&amp; <span class=\"hljs-built_in\">std<\/span>::next(current) != songs.end()) {\n            ++current;\n            playCurrent();\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"End of playlist.\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">previous<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (current != songs.begin()) {\n            --current;\n            playCurrent();\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Start of playlist.\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n        }\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;Song&gt; songs;\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;Song&gt;::iterator current;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-47\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Managing Tasks in a To-do List Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For a simple to-do list application, tasks can be added, marked as done, or removed.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-48\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ToDoList<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">addTask<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">string<\/span>&amp; task)<\/span> <\/span>{\n        tasks.push_back(task);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">markDone<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span> index)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (index &gt;= <span class=\"hljs-number\">0<\/span> &amp;&amp; index &lt; tasks.size()) {\n            <span class=\"hljs-keyword\">auto<\/span> it = <span class=\"hljs-built_in\">std<\/span>::next(tasks.begin(), index);\n            tasks.erase(it);\n            <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Task marked as done.\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"Invalid task index.\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">showTasks<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> index = <span class=\"hljs-number\">0<\/span>;\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-keyword\">auto<\/span>&amp; task : tasks) {\n            <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; index++ &lt;&lt; <span class=\"hljs-string\">\". \"<\/span> &lt;&lt; task &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n        }\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">string<\/span>&gt; tasks;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-48\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Creating a Basic Undo-Redo Functionality Using <code>std::list<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A classic use of a doubly-linked list is to manage a series of states in an application to allow undo and redo functionality.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-49\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">template<\/span> &lt;<span class=\"hljs-keyword\">typename<\/span> T&gt;\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">UndoRedoList<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">addAction<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">const<\/span> T&amp; action)<\/span> <\/span>{\n        <span class=\"hljs-comment\">\/\/ Remove any \"future\" actions if we're in the middle of the list<\/span>\n        actions.erase(<span class=\"hljs-built_in\">std<\/span>::next(current), actions.end());\n\n        <span class=\"hljs-comment\">\/\/ Add the new action and set it as the current<\/span>\n        actions.push_back(action);\n        current = <span class=\"hljs-built_in\">std<\/span>::prev(actions.end());\n    }\n\n    <span class=\"hljs-function\">T <span class=\"hljs-title\">undo<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (current == actions.begin()) {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-built_in\">std<\/span>::runtime_error(<span class=\"hljs-string\">\"Nothing to undo.\"<\/span>);\n        }\n        --current;\n        <span class=\"hljs-keyword\">return<\/span> *current;\n    }\n\n    <span class=\"hljs-function\">T <span class=\"hljs-title\">redo<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-built_in\">std<\/span>::next(current) == actions.end()) {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-built_in\">std<\/span>::runtime_error(<span class=\"hljs-string\">\"Nothing to redo.\"<\/span>);\n        }\n        ++current;\n        <span class=\"hljs-keyword\">return<\/span> *current;\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;T&gt; actions;\n    <span class=\"hljs-keyword\">typename<\/span> <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">list<\/span>&lt;T&gt;::iterator current = actions.end();\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-49\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">These examples show the versatility of <code>std::list<\/code>. Depending on the use case, this container can offer efficient solutions for a wide range of tasks, especially when insertions or deletions in the middle of a sequence are frequent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The decision to use <code>std::list<\/code> should stem from an informed perspective. While it&#8217;s a go-to container for specific scenarios, other STL containers might be more appropriate for different tasks. The goal is to strike a balance between the container&#8217;s strengths and the specific needs of the application at hand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction What is std::list? std::list is one of the sequence containers provided by the Standard Template Library (STL) in C++, that allows the storage and manipulation of a doubly-linked list of elements. Each element in the list is stored in a node, and each node contains a pointer to the previous node and the next [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[9,4],"tags":[],"class_list":["post-1226","post","type-post","status-publish","format-standard","category-cplusplus","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ std::list (&lt;list&gt;)- From Basics to Advanced<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ std::list (&lt;list&gt;)- From Basics to Advanced\" \/>\n<meta property=\"og:description\" content=\"Introduction What is std::list? std::list is one of the sequence containers provided by the Standard Template Library (STL) in C++, that allows the storage and manipulation of a doubly-linked list of elements. Each element in the list is stored in a node, and each node contains a pointer to the previous node and the next [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-06T12:06:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-02T06:42:54+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"C++ std::list (&lt;list&gt;)- From Basics to Advanced\",\"datePublished\":\"2023-09-06T12:06:57+00:00\",\"dateModified\":\"2023-10-02T06:42:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/\"},\"wordCount\":2689,\"commentCount\":0,\"articleSection\":[\"C++\",\"Programming Languages\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/\",\"name\":\"C++ std::list (&lt;list&gt;)- From Basics to Advanced\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2023-09-06T12:06:57+00:00\",\"dateModified\":\"2023-10-02T06:42:54+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/c-stdlist-from-basics-to-advanced\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"C++ std::list (&lt;list&gt;)- From Basics to Advanced\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ std::list (&lt;list&gt;)- From Basics to Advanced","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:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/","og_locale":"en_US","og_type":"article","og_title":"C++ std::list (&lt;list&gt;)- From Basics to Advanced","og_description":"Introduction What is std::list? std::list is one of the sequence containers provided by the Standard Template Library (STL) in C++, that allows the storage and manipulation of a doubly-linked list of elements. Each element in the list is stored in a node, and each node contains a pointer to the previous node and the next [&hellip;]","og_url":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/","article_published_time":"2023-09-06T12:06:57+00:00","article_modified_time":"2023-10-02T06:42:54+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"C++ std::list (&lt;list&gt;)- From Basics to Advanced","datePublished":"2023-09-06T12:06:57+00:00","dateModified":"2023-10-02T06:42:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/"},"wordCount":2689,"commentCount":0,"articleSection":["C++","Programming Languages"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/","url":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/","name":"C++ std::list (&lt;list&gt;)- From Basics to Advanced","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2023-09-06T12:06:57+00:00","dateModified":"2023-10-02T06:42:54+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/c-stdlist-from-basics-to-advanced\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"C++ std::list (&lt;list&gt;)- From Basics to Advanced"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=1226"}],"version-history":[{"count":10,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1226\/revisions"}],"predecessor-version":[{"id":1543,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1226\/revisions\/1543"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=1226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=1226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=1226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}