{"id":2171,"date":"2025-01-28T19:41:24","date_gmt":"2025-01-28T19:41:24","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=2171"},"modified":"2025-01-28T21:36:07","modified_gmt":"2025-01-28T21:36:07","slug":"generic-algorithms","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/the-standard-template-library-2\/generic-algorithms\/","title":{"rendered":"Generic Algorithms"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generic_Algorithms\"><\/span>Generic Algorithms<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We&#8217;ve already seen one generic algorithm: <strong>reverse()<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"find\"><\/span>find()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another generic algorithm is <strong>find()<\/strong>.<\/p>\n\n\n\n<p>Here&#8217;s an example using a regular array of chars:<\/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);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  FindExample.cpp\n\/\/  STL Sequence Containers\n\/\/\n\/\/  Created by Bryan Higgs on 10\/20\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;algorithm&gt;  \/\/ For find()\n\nint main(int argc, const char * argv[])\n{\n  char s[] = &quot;Example of generic find algorithm&quot;;\n  size_t len = strlen(s);\n  const char *foundChar = std::find(&amp;s[0], &amp;s[len], 'g');\n  std::cout &lt;&lt; &quot;Found character: &quot; &lt;&lt; *foundChar &lt;&lt; std::endl\n            &lt;&lt; &quot;Next character: &quot; &lt;&lt; *(foundChar+1) &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\">\/\/  FindExample.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Sequence Containers<\/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\/20\/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;algorithm&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For find()<\/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\">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: #000000\">  <\/span><span style=\"color: #0000FF\">size_t<\/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: #000000\">  <\/span><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: #267F99\">std<\/span><span style=\"color: #000000\">::<\/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: #000000\">  <\/span><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\">            &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>\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 outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Found character: g\nNext character: e\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>Here&#8217;s how you can use <strong>find()<\/strong> with a <strong>std::vector<\/strong> :<\/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);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  FindExample2.cpp\n\/\/  STL Sequence Containers\n\/\/\n\/\/  Created by Bryan Higgs on 10\/20\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;algorithm&gt;  \/\/ For find()\n\n#include &quot;Make.h&quot;\n\nint main(int argc, const char * argv[])\n{\n  std::vector&lt;char&gt; v = make&lt; std::vector&lt;char&gt; &gt;(\n        &quot;Example of generic find algorithm&quot;);\n  std::vector&lt;char&gt;::iterator foundChar\n        = find(v.begin(), v.end(), 'g');\n  std::cout &lt;&lt; &quot;Found character: &quot; &lt;&lt; *foundChar &lt;&lt; std::endl\n            &lt;&lt; &quot;Next character: &quot; &lt;&lt; *(foundChar+1) &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\">\/\/  FindExample2.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Sequence Containers<\/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\/20\/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;algorithm&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For find()<\/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\">&quot;Make.h&quot;<\/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: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt; v = <\/span><span style=\"color: #795E26\">make<\/span><span style=\"color: #000000\">&lt; <\/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\">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 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\">char<\/span><span style=\"color: #000000\">&gt;::iterator foundChar<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        = <\/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\">  <\/span><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\">            &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>\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 the exact same thing as the previous example:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Found character: g\nNext character: e\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=\"merge\"><\/span>merge()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another generic algorithm from the STL is <strong>merge()<\/strong>, which combines the contents of two sorted sequences into a single sorted sequence:<\/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);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  MergeExample.cpp\n\/\/  STL Sequence Containers\n\/\/\n\/\/  Created by Bryan Higgs on 10\/20\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;list&gt;     \/\/ For list\n#include &lt;deque&gt;    \/\/ For deque\n#include &lt;algorithm&gt;\/\/ For merge\n\n#include &quot;make.h&quot;\n\nint main(int argc, const char * argv[])\n{\n  char vowels[] = &quot;aeiou&quot;;\n  size_t len = strlen(vowels);\n  std::cout &lt;&lt; &quot;Vowels: &quot; &lt;&lt; vowels &lt;&lt; std::endl;\n  \n  std::list&lt;char&gt; consonants\n    = make&lt; std::list&lt;char&gt; &gt;(&quot;bcdfghjklmnpqrstvwxyz&quot;);\n  std::cout &lt;&lt; &quot;Consonants: &quot;;\n  for (char c: consonants)\n  {\n    std::cout &lt;&lt; c;\n  }\n  std::cout &lt;&lt; std::endl;\n  \n  std::deque&lt;char&gt; letters(26, 'X');\n  std::cout &lt;&lt; &quot;Letters (initial) : &quot;;\n  for (char c: letters)\n  {\n    std::cout &lt;&lt; c;\n  }\n  std::cout &lt;&lt; std::endl;\n  \n  merge(&amp;vowels[0], &amp;vowels[len],\n        consonants.begin(), consonants.end(),\n        letters.begin()\n        );\n  std::cout &lt;&lt; &quot;Letters (final) : &quot;;\n  for (char c: letters)\n  {\n    std::cout &lt;&lt; c;\n  }\n  std::cout &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\">\/\/  MergeExample.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Sequence Containers<\/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\/20\/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;list&gt;<\/span><span style=\"color: #0000FF\">     <\/span><span style=\"color: #008000\">\/\/ For list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;deque&gt;<\/span><span style=\"color: #0000FF\">    <\/span><span style=\"color: #008000\">\/\/ For deque<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;algorithm&gt;<\/span><span style=\"color: #008000\">\/\/ For merge<\/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\">&quot;make.h&quot;<\/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\">char<\/span><span style=\"color: #000000\"> vowels[] = <\/span><span style=\"color: #A31515\">&quot;aeiou&quot;<\/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: #795E26\">strlen<\/span><span style=\"color: #000000\">(vowels);<\/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;Vowels: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; vowels &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: #267F99\">std<\/span><span style=\"color: #000000\">::list&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt; consonants<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    = <\/span><span style=\"color: #795E26\">make<\/span><span style=\"color: #000000\">&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">list<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt; &gt;(<\/span><span style=\"color: #A31515\">&quot;bcdfghjklmnpqrstvwxyz&quot;<\/span><span style=\"color: #000000\">);<\/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;Consonants: &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> c: consonants)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; c;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/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: #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: #267F99\">std<\/span><span style=\"color: #000000\">::deque&lt;<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">&gt; <\/span><span style=\"color: #795E26\">letters<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">26<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&#39;X&#39;<\/span><span style=\"color: #000000\">);<\/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;Letters (initial) : &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> c: letters)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; c;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/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: #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: #795E26\">merge<\/span><span style=\"color: #000000\">(&amp;<\/span><span style=\"color: #001080\">vowels<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">], &amp;<\/span><span style=\"color: #001080\">vowels<\/span><span style=\"color: #000000\">[len],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        <\/span><span style=\"color: #001080\">consonants<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">consonants<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        <\/span><span style=\"color: #001080\">letters<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/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: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Letters (final) : &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> c: letters)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; c;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/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: #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 outputs the exact same thing as the previous example:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Vowels: aeiou\nConsonants: bcdfghjklmnpqrstvwxyz\nLetters (initial) : XXXXXXXXXXXXXXXXXXXXXXXXXX\nLetters (final) : abcdefghijklmnopqrstuvwxyz\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\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\/iterators\/\">Next: Iterators<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Generic Algorithms We&#8217;ve already seen one generic algorithm: reverse(). find() Another generic algorithm is find(). Here&#8217;s an example using a regular array of chars: &#8230; which outputs: Here&#8217;s how you can use find() with a std::vector : &#8230; which outputs the exact same thing as the previous example: merge() Another generic algorithm from the STL [&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-2171","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2171","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=2171"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2171\/revisions"}],"predecessor-version":[{"id":2221,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2171\/revisions\/2221"}],"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=2171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}