{"id":2177,"date":"2025-01-28T19:46:48","date_gmt":"2025-01-28T19:46:48","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=2177"},"modified":"2025-01-28T21:37:41","modified_gmt":"2025-01-28T21:37:41","slug":"iterators","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/the-standard-template-library-2\/iterators\/","title":{"rendered":"Iterators"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Iterators\"><\/span>Iterators<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>An iterator is&nbsp;<em>an object (like a pointer) that points to an element inside the&nbsp;container.<\/em> <\/p>\n\n\n\n<p>We can use iterators to move through the contents of the container. They can be seen as something similar to a pointer pointing to some location and, using them, we can access content at that particular location.<\/p>\n\n\n\n<p><em>Iterators<\/em> are key to using STL effectively.<\/p>\n\n\n\n<p>One kind of iterator is <em><strong>an ordinary C++ pointer<\/strong><\/em>.&nbsp; We were using a C++ pointer iterator in the code:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"char s[] = &quot;Example of generic find algorithm&quot;;\nint len = strlen(s);\nconst char *foundChar = find(&amp;s[0], &amp;s[len], 'g');\nstd::cout &lt;&lt; &quot;Found character: &quot; &lt;&lt; *foundChar &lt;&lt; std::endl\n\t        &lt;&lt; &quot;Next character: &quot; &lt;&lt; *(foundChar+1) &lt;&lt; std::endl;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> s[] = <\/span><span style=\"color: #A31515\">&quot;Example of generic find algorithm&quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> len = <\/span><span style=\"color: #795E26\">strlen<\/span><span style=\"color: #000000\">(s);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *foundChar = <\/span><span style=\"color: #795E26\">find<\/span><span style=\"color: #000000\">(&amp;<\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">], &amp;<\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">[len], <\/span><span style=\"color: #A31515\">&#39;g&#39;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Found character: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *foundChar &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t        &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Next character: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *(foundChar+<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">) &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>There are other kinds of iterators, but they are required to behave in a way similar to pointers. <\/p>\n\n\n\n<p>For example, the <strong>operator <code>*<\/code><\/strong> dereferences the iterator to the value to which it refers.<\/p>\n\n\n\n<p>We were using other kinds of iterator in the code:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"std::vector&lt;char&gt; v = make&lt; std::vector&lt;char&gt; &gt;\n(&quot;Example of generic find algorithm&quot;);\n\nstd::vector&lt;char&gt;::iterator foundChar \n\t\t\t= find(v.begin(), v.end(), 'g');\n\t\t\t\nstd::cout &lt;&lt; &quot;Found character: &quot; &lt;&lt; *foundChar &lt;&lt; std::endl\n\t        &lt;&lt; &quot;Next character: &quot; &lt;&lt; *(foundChar+1) &lt;&lt; std::endl;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt; v = make&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt; &gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Example of generic find algorithm&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">vector<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt;::iterator foundChar <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t\t\t= <\/span><span style=\"color: #795E26\">find<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #A31515\">&#39;g&#39;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t\t\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Found character: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *foundChar &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t        &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Next character: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *(foundChar+<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">) &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_using_accumulate\"><\/span>Example using accumulate()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s an example of using an Iterator with the Generic Algorithm <strong>accumulate()<\/strong><\/p>\n\n\n\n<p><strong>accumulate()<\/strong> returns the result of accumulating all the values in the specified range.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  STL Iterators\n\/\/\n\/\/  Created by Bryan Higgs on 10\/21\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;   \/\/ For vector\n#include &lt;numeric&gt;  \/\/ For accumulate()\n\nint main(int argc, const char * argv[])\n{\n  int values[] = {2, 3, 5, 7, 11};\n  int len = sizeof(values)\/sizeof(values[0]);\n  \n  \/\/ Initialize vector using pointer iterator\n  std::vector&lt;int&gt; v(&amp;values[0], &amp;values[len]);\n\n  int sum = accumulate(v.begin(), v.end(), 0);\n\n  std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; sum &lt;&lt; std::endl;\n\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Iterators<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/21\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;vector&gt;<\/span><span style=\"color: #0000FF\">   <\/span><span style=\"color: #008000\">\/\/ For vector<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;numeric&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For accumulate()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> values[] = {<\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">7<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">11<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> len = <\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(values)\/<\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Initialize vector using pointer iterator<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt; <\/span><span style=\"color: #795E26\">v<\/span><span style=\"color: #000000\">(&amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">], &amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[len]);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> sum = <\/span><span style=\"color: #795E26\">accumulate<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; sum &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>&#8230; which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Result: 28\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Iterator_Categories\"><\/span>Iterator Categories<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>There are several categories of iterators:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Input<\/em> Iterators<\/li>\n\n\n\n<li><em>Output<\/em> Iterators<\/li>\n\n\n\n<li><em>Forward<\/em> Iterators<\/li>\n\n\n\n<li><em>Bidirectional<\/em> Iterators<\/li>\n\n\n\n<li><em>Random Access<\/em> Iterators<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>These iterator categories form a hierarchy<br>(<em>Note: this is not an inheritance hierarchy.<\/em>):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"303\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/10\/CIteratorHierarchy.png\" alt=\"\" class=\"wp-image-1482\" srcset=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/10\/CIteratorHierarchy.png 593w, https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/10\/CIteratorHierarchy-300x153.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>C++ iterator categories define the capabilities of an iterator, influencing the operations you can perform with it. <\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes dark-header dark-first-column\"><table class=\"has-palette-color-7-background-color has-background\" style=\"border-width:2px\"><thead><tr><th><\/th><th>Output<\/th><th>Input<\/th><th>Forward<\/th><th>Bi-directional<\/th><th>Random Access<\/th><\/tr><\/thead><tbody><tr><td><strong>Read<\/strong><\/td><td><\/td><td>x = *i<\/td><td>x = *i<\/td><td>x = *i<\/td><td>x = *i<\/td><\/tr><tr><td><strong>Write<\/strong><\/td><td>*i = x<\/td><td><\/td><td>*i = x<\/td><td>*i = x<\/td><td>*i = x<\/td><\/tr><tr><td><strong>Iteration<\/strong><\/td><td>++<\/td><td>++<\/td><td>++<\/td><td>++, &#8212;<\/td><td>++, &#8211;, +, -, <br>+=, -=<\/td><\/tr><tr><td><strong>Comparison<\/strong><\/td><td><\/td><td>==, !=<\/td><td>==, !=<\/td><td>==, !=<\/td><td>==, !=, &lt;, &gt;,<br>&lt;=, &gt;=<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">The set of operations allowed for each iterator category<\/figcaption><\/figure>\n\n\n\n<p>Here&#8217;s a breakdown of the categories and their associated operations:<\/p>\n\n\n\n<p><strong>Iterator Categories<\/strong> (in increasing order of power):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Output Iterator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Can only be incremented (<code>++<\/code>).<\/li>\n\n\n\n<li>Can be assigned to (<code>*it = value<\/code>).<\/li>\n\n\n\n<li>Used for writing data to a sequence.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input Iterator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Can only be incremented (<code>++<\/code>).<\/li>\n\n\n\n<li>Can be dereferenced to read the value (<code>*it<\/code>).<\/li>\n\n\n\n<li>Can be compared for equality (<code>==<\/code>, <code>!=<\/code>).<\/li>\n\n\n\n<li>Used for reading data from a sequence.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Forward Iterator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Combines the capabilities of input and output iterators.<\/li>\n\n\n\n<li>Can be dereferenced to read and write (<code>*it<\/code>).<\/li>\n\n\n\n<li>Can be compared for equality (<code>==<\/code>, <code>!=<\/code>).<\/li>\n\n\n\n<li>Can be used in algorithms that require multiple passes over a sequence.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Bidirectional Iterator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Inherits all the capabilities of a forward iterator.<\/li>\n\n\n\n<li>Can also be decremented (<code>--<\/code>).<\/li>\n\n\n\n<li>Used for traversing a sequence in both directions.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Random Access Iterator:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Inherits all the capabilities of a bidirectional iterator.<\/li>\n\n\n\n<li>Can be used with arithmetic operators (<code>+<\/code>, <code>-<\/code>, <code>+=<\/code>, <code>-=<\/code>) to move a fixed number of positions.<\/li>\n\n\n\n<li>Can be used with subscript operator (<code>[]<\/code>) for random access to elements.<\/li>\n\n\n\n<li>Can be compared with relational operators (<code>&lt;<\/code>, <code>&gt;<\/code>, <code>&lt;=<\/code>, <code>&gt;=<\/code>).<\/li>\n\n\n\n<li>Used for efficient access to any element in a sequence.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>As you can see, Random Access iterators are the most powerful and flexible of all the iterators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Iterator_Bounds\"><\/span>Iterator Bounds<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The idiom typically used with pointers is:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"int array[] = { ... };\nint len = sizeof(array)\/sizeof(array[0]);\nfor ( int *p = &amp;array[0]; p &lt; &amp;array[len]; p++\t)\n{\n\t\/\/ ...\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> array[] = { ... };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> len = <\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(array)\/<\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">array<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> ( <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> *p = &amp;<\/span><span style=\"color: #001080\">array<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">]; p &lt; &amp;<\/span><span style=\"color: #001080\">array<\/span><span style=\"color: #000000\">[len]; p++\t)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Note, that the <strong>p &lt; &amp;array[len]<\/strong> means &#8220;one past the end&#8221;\/<\/p>\n<\/blockquote>\n\n\n\n<p>Similarly, with generalized iterators, <strong>end()<\/strong> means &#8220;one past the end&#8221;:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"std::vector&lt;int&gt; v;\n\/\/...\nfor (std::vector&lt;int&gt;::iterator i = v.begin(); i &lt; v.end(); i++)\n{\n\t\/\/ ...\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt; v;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">vector<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt;::iterator i = <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(); i &lt; <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(); i++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generalizing_accumulate\"><\/span>Generalizing accumulate()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The earlier <strong>accumulate()<\/strong> function is not as general as it might be.&nbsp; It assumes that there is a <strong>+<\/strong> operator defined on the type <strong>T<\/strong>, because of its use in the expression:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>init = init + *first;<\/strong><\/code><\/pre>\n\n\n\n<p>The version of <strong>accumulate()<\/strong> we have already seen is:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>accumulate(beg, end, init)<\/strong><\/code><\/pre>\n\n\n\n<p>which returns the sum of all the values in the input range { <strong>beg<\/strong> : <strong>end<\/strong> }. <\/p>\n\n\n\n<p>The summation starts with the initial value specified by <strong>init<\/strong>.<\/p>\n\n\n\n<p>There is another version of <strong>accumulate()<\/strong> which provides a more generalized form:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>accumulate(beg, end, init, BinaryOp)<\/strong><\/code><\/pre>\n\n\n\n<p>The first version of accumulate applies the <strong>+<\/strong> operator for the element type; the second version applies the specified binary operation.<\/p>\n\n\n\n<p>We can use this second form of <strong>accumulate()<\/strong> like this:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  STL accumulate\n\/\/\n\/\/  Created by Bryan Higgs on 10\/23\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;numeric&gt;  \/\/ For accumulate()\n\n\/\/ The binary operation for multiply\nint mult(int x, int y)\n{\n  return x * y;\n}\n\nint main(int argc, const char * argv[])\n{\n  int values[] = {2, 3, 5, 7, 11};\n  size_t len = sizeof(values)\/sizeof(values[0]);\n  \n  \/\/ Initialize vector using pointer iterator\n  std::vector&lt;int&gt; v(&amp;values[0], &amp;values[len]);\n  \n  int product = accumulate(v.begin(), v.end(), 1, mult);\n  std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; product &lt;&lt; std::endl;\n  \n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL accumulate<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;vector&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For vector<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;numeric&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For accumulate()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ The binary operation for multiply<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">mult<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">y<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> x * y;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> values[] = {<\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">7<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">11<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">size_t<\/span><span style=\"color: #000000\"> len = <\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(values)\/<\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Initialize vector using pointer iterator<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt; <\/span><span style=\"color: #795E26\">v<\/span><span style=\"color: #000000\">(&amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">], &amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[len]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> product = <\/span><span style=\"color: #795E26\">accumulate<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">, mult);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; product &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>&#8230; which produces the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Result: 2310\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n\n\n\n<p>In other words, the product, rather than the sum.<\/p>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:47px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/the-standard-template-library-2\/function-objects\/\">Next: Function Objects<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Iterators An iterator is&nbsp;an object (like a pointer) that points to an element inside the&nbsp;container. We can use iterators to move through the contents of the container. They can be seen as something similar to a pointer pointing to some location and, using them, we can access content at that particular location. Iterators are key [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2150,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2177","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=2177"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2177\/revisions"}],"predecessor-version":[{"id":2223,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2177\/revisions\/2223"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2150"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=2177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}