{"id":2202,"date":"2025-01-28T20:35:49","date_gmt":"2025-01-28T20:35:49","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=2202"},"modified":"2025-01-28T21:45:27","modified_gmt":"2025-01-28T21:45:27","slug":"function-adapters","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/the-standard-template-library-2\/function-adapters\/","title":{"rendered":"Function Adapters"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Function_Adapters\"><\/span>Function Adapters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Just as iterator adapters help you construct a wider variety of iterators, a <em><strong>Function Adapter<\/strong><\/em> helps you create a wider variety of function objects.<\/p>\n\n\n\n<p>STL provides three kinds of function adapters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em><strong>Binders<\/strong><\/em> : Converts function objects that take two arguments into function objects that take one.<\/li>\n\n\n\n<li><em><strong>Negaters<\/strong><\/em> : Create a new function object class that inverts the result of a function.<\/li>\n\n\n\n<li><em><strong>Adapters for pointers to functions<\/strong><\/em> : Useful if you already have a function defined, and want to create a function object out of it.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\"><strong>WARNING:<\/strong><\/p>\n\n\n\n<p>It appears that a number of these function adapters are now deprecated in C++11, and removed in C++17. <\/p>\n\n\n\n<p>There are alternative approaches recommended below, and on the web.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Binders\"><\/span>Binders<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In the C++ STL, <em><strong>binders<\/strong><\/em> are a type of functors that<strong>&nbsp;bind or associate some of the arguments of a function to a fixed value.<\/strong>&nbsp;This fixed value is stored inside the functor and the rest of the arguments can be passed dynamically at the time of calling the functor.<\/p>\n\n\n\n<p>Here is an example of using a <em><strong>binder<\/strong><\/em>.<\/p>\n\n\n\n<p>The <strong>find_if()<\/strong> algorithm is like <strong>find()<\/strong>, except that it takes a unary predicate function object (a function object encapsulating a one-argument function that returns a <strong>bool<\/strong> value.)<\/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 Function Adapters\n\/\/\n\/\/  Created by Bryan Higgs on 10\/24\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n#include &lt;functional&gt;\n\n\/\/ Binder example\nint main(int argc, const char * argv[])\n{\n  std::vector&lt;int&gt; values = { 10, -200, 30, -90, 56, 89 };\n  \n  \/\/ Find all elements greater than 20\n  std::vector&lt;int&gt;::iterator it =\n          std::find_if(values.begin(), values.end(),\n                       std::bind2nd(std::greater&lt;int&gt;(), 20));\n  if (it != values.end())\n  {\n    std::cout &lt;&lt; &quot;First element greater than 20: &quot; &lt;&lt; *it &lt;&lt; std::endl;\n  }\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 Function Adapters<\/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\/24\/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>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;algorithm&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;functional&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Binder example<\/span><\/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\">int<\/span><span style=\"color: #000000\">&gt; values = { <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">, -<\/span><span style=\"color: #098658\">200<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">30<\/span><span style=\"color: #000000\">, -<\/span><span style=\"color: #098658\">90<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">56<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">89<\/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\">  \/\/ Find all elements greater than 20<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><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 it =<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">          <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">find_if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(),<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                       <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">bind2nd<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">greater<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt;(), <\/span><span style=\"color: #098658\">20<\/span><span style=\"color: #000000\">));<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (it != <\/span><span style=\"color: #001080\">values<\/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>\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;First element greater than 20: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *it &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>\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>First element greater than 20: 30\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>We constructed the predicate for the search by starting with <strong>greater&lt;int&gt;()<\/strong>, a function object that defines a binary function:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>bool operator()(int x, int y) const\n{ return x &gt; y; }<\/strong><\/code><\/pre>\n\n\n\n<p>By applying <strong>bind2nd<\/strong> to this function object, and <strong>50<\/strong>, we produced a function object that defines a unary function:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>bool operator()(int x) const\n{ return x &gt; 50; }<\/strong><\/code><\/pre>\n\n\n\n<p>as if we had programmed another function object type:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>struct GreaterThan50 : unary_function&lt;int, bool&gt;\n{\n\tbool operator()(int x) const\n\t{ return x &gt; 50; }\n};<\/strong><\/code><\/pre>\n\n\n\n<p>The <strong>bind2nd<\/strong> binder binds the second argument of the binary operator.&nbsp; There is also a <strong>bind1st<\/strong> binder which may be used to bind the first argument in a similar way<\/p>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\"><strong>NOTE:<\/strong><\/p>\n\n\n\n<p><strong>bind1st<\/strong>&nbsp;and&nbsp;<strong>bind2nd<\/strong>&nbsp;are deprecated in C++11 and removed in C++17.<\/p>\n\n\n\n<p>The above code was compiled and run using C++11, and produced the following warning:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>main.cpp:21:29 'bind2nd&lt;std::greater&lt;int&gt;, int&gt;' is deprecated<\/strong>\n<strong>bind2nd&lt;std::greater&lt;int&gt;, int&gt;' has been explicitly marked deprecated here<\/strong><\/code><\/pre>\n\n\n\n<p>It refused to compile using C++17.<\/p>\n\n\n\n<p>Starting from C++11, the C++ Standard Library provides&nbsp;<strong>std::bind,<\/strong>&nbsp;which is a more general binder that can bind any number of arguments to any values and returns a new function object. The new function object can then be used as a regular function, taking the remaining unbound arguments.<\/p>\n\n\n\n<p><strong>Syntax of std::bind in C++<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px;font-style:normal;font-weight:700\"><code><strong>std::bind(function, arg1, arg2, ..., argN);<\/strong><\/code><\/pre>\n\n\n\n<p><strong>Parameters in std::bind in C++<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>function&nbsp;<\/strong>is the function or function object that you want to bind some of its arguments.<\/li>\n\n\n\n<li><strong>arg1, arg2, \u2026, argN&nbsp;<\/strong>are the arguments to bind. These arguments can be any type of value or expression, including references, pointers, and placeholder values (e.g., std::placeholders::_1, std::placeholders::_2, etc.)<\/li>\n<\/ul>\n\n\n\n<p>The current advice is to <strong>&#8220;<em>consider using lambdas instead, which are more flexible and readable<\/em>.&#8221;<\/strong> (For lambda descriptions, see later)<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Negaters\"><\/span>Negaters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <em>Negater<\/em> is a kind of function adapter that is used to reverse the sense of a predicate function object.<\/p>\n\n\n\n<p>STL provides two negaters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>not1<\/strong> : Negates a unary predicate<\/li>\n\n\n\n<li><strong>not2<\/strong> : Negates a binary predicate<\/li>\n<\/ul>\n\n\n\n<p>The <strong>not1<\/strong> negater, given a unary predicate function object <strong>p<\/strong>, generates a unary predicate object that defines the function:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>bool operator()(const T &amp;x) const\n{ return !(p(x)); }<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_not1_Negater\"><\/span>The <code>not1<\/code> Negater<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here is an example of a <strong>not1<\/strong> negator:<\/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 Function Adapters\n\/\/\n\/\/  Created by Bryan Higgs on 10\/24\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n#include &lt;functional&gt;\n\n\/\/ not1 Negator example\nint main(int argc, const char * argv[])\n{\n  std::vector&lt;int&gt; values = { 10, -200, 30, -90, 56, 89 };\n  \n  \/\/ Find all elements greater than 20\n  std::vector&lt;int&gt;::iterator it =\n  std::find_if(values.begin(), values.end(),\n               not1(\n                    std::bind2nd(std::greater&lt;int&gt;(), 20)\n                    )\n               );\n  if (it != values.end())\n  {\n    std::cout &lt;&lt; &quot;First element not greater than 20: &quot; &lt;&lt; *it &lt;&lt; std::endl;\n  }\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 Function Adapters<\/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\/24\/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>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;algorithm&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;functional&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ not1 Negator example<\/span><\/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\">int<\/span><span style=\"color: #000000\">&gt; values = { <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">, -<\/span><span style=\"color: #098658\">200<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">30<\/span><span style=\"color: #000000\">, -<\/span><span style=\"color: #098658\">90<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">56<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">89<\/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\">  \/\/ Find all elements greater than 20<\/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\">int<\/span><span style=\"color: #000000\">&gt;::iterator it =<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">find_if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(),<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">               <\/span><span style=\"color: #795E26\">not1<\/span><span style=\"color: #000000\">(<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">bind2nd<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">greater<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt;(), <\/span><span style=\"color: #098658\">20<\/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>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (it != <\/span><span style=\"color: #001080\">values<\/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>\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;First element not greater than 20: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *it &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>\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:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>First element not greater than 20: 10\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>This code was compiled using C++11, with the <strong><code>bind2nd<\/code><\/strong> deprecation warning.<\/p>\n\n\n\n<p>It will not compile using C++17<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_not2_Negater\"><\/span>The not2 Negater<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Assume we have a class, <strong>CompareIDs<\/strong>, of objects with a binary predicate function object that compares objects based on an <strong>id<\/strong> field.<\/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\/\/  CompareIDs.h\n\/\/  STL Function Adapters\n\/\/\n\/\/  Created by Bryan Higgs on 10\/24\/24.\n\/\/\n\n#ifndef CompareIDs_h\n#define CompareIDs_h\n\n#include &lt;iostream&gt;   \/\/ for ostream, etc.\n#include &lt;functional&gt; \/\/ for binary_function\n\nclass CompareIDs\n  : public std::binary_function&lt;CompareIDs, CompareIDs, bool&gt;\n{\npublic:\n  int id;  \/\/ ID field\n  bool operator()(const CompareIDs &amp;x, const CompareIDs &amp;y) const\n  {\n    return x.id &gt;= y.id;\n  }\n  \n  friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;o, const CompareIDs &amp;x)\n  {\n    o &lt;&lt; x.id;\n    return o;\n  }\n};\n\n#endif \/* CompareIDs_h *\/\" 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\">\/\/  CompareIDs.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Function Adapters<\/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\/24\/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\">#ifndef<\/span><span style=\"color: #0000FF\"> CompareIDs_h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#define<\/span><span style=\"color: #0000FF\"> CompareIDs_h<\/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 style=\"color: #0000FF\">   <\/span><span style=\"color: #008000\">\/\/ for ostream, etc.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;functional&gt;<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #008000\">\/\/ for binary_function<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">CompareIDs<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  : <\/span><span style=\"color: #0000FF\">public<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">binary_function<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #267F99\">CompareIDs<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #267F99\">CompareIDs<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">bool<\/span><span style=\"color: #000000\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> id;<\/span><span style=\"color: #008000\">  \/\/ ID field<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">bool<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #AF00DB\">operator()<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">CompareIDs<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">CompareIDs<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">y<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000FF\">const<\/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: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">id<\/span><span style=\"color: #000000\"> &gt;= <\/span><span style=\"color: #001080\">y<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">id<\/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>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">friend<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">ostream<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #AF00DB\">operator&lt;&lt;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">ostream<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">o<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">CompareIDs<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">x<\/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\">    o &lt;&lt; <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">id<\/span><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\"> o;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#endif<\/span><span style=\"color: #008000\"> \/* CompareIDs_h *\/<\/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>Assume we want to sort a vector of <strong>CompareIDs<\/strong> objects<\/p>\n\n\n\n<p>We can&#8217;t use default ordering, since operator <strong>&lt;<\/strong> is not defined for <strong>CompareIDs<\/strong>.<\/p>\n\n\n\n<p>We can&#8217;t use the overloaded operator defined in the type, because it uses <strong>&gt;=<\/strong> to compare ids, not <strong>&lt;<\/strong><\/p>\n\n\n\n<p>We can, however, use a <strong>not2<\/strong> negator to solve the problem:<\/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 Function Adapters\n\/\/\n\/\/  Created by Bryan Higgs on 10\/24\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n#include &lt;functional&gt;\n\n#include &quot;compareIDs.h&quot;\n\nstatic const size_t SIZE = 15;\n\nvoid display(std::vector&lt;CompareIDs&gt; v)\n{\n  for (int i = 0; i &lt; SIZE; i++)\n  {\n    std::cout &lt;&lt; v[i] &lt;&lt; std::endl;\n  }\n}\n\nint main(int argc, const char * argv[])\n{\n  std::vector&lt;CompareIDs&gt; v(SIZE);\n  \n  for (int i = 0; i &lt; SIZE; i++)\n  {\n    v[i].id = SIZE - (i + 1);\n  }\n  \n  std::cout &lt;&lt; &quot;Before sorting:&quot; &lt;&lt; std::endl;\n  display(v);\n  std::cout &lt;&lt; std::endl;\n  \n  sort( v.begin(), v.end(), not2(CompareIDs()) );\n  \n  std::cout &lt;&lt; &quot;After sorting:&quot; &lt;&lt; std::endl;\n  display(v);\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\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Function Adapters<\/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\/24\/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>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;algorithm&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;functional&gt;<\/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;compareIDs.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">size_t<\/span><span style=\"color: #000000\"> SIZE = <\/span><span style=\"color: #098658\">15<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">display<\/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: #267F99\">CompareIDs<\/span><span style=\"color: #000000\">&gt; <\/span><span style=\"color: #001080\">v<\/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: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; i &lt; SIZE; i++)<\/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: #001080\">v<\/span><span style=\"color: #000000\">[i] &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>\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;CompareIDs&gt; <\/span><span style=\"color: #795E26\">v<\/span><span style=\"color: #000000\">(SIZE);<\/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\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; i &lt; SIZE; i++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">[i].<\/span><span style=\"color: #001080\">id<\/span><span style=\"color: #000000\"> = SIZE - (i + <\/span><span style=\"color: #098658\">1<\/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>\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;Before sorting:&quot;<\/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 style=\"color: #795E26\">display<\/span><span style=\"color: #000000\">(v);<\/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 cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">sort<\/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: #795E26\">not2<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #795E26\">CompareIDs<\/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;After sorting:&quot;<\/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 style=\"color: #795E26\">display<\/span><span style=\"color: #000000\">(v);<\/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 produces the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Before sorting:\n14\n13\n12\n11\n10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n0\n\nAfter sorting:\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\"><strong>NOTE:<\/strong><\/p>\n\n\n\n<p>The above program was compiled using C++11, and it received the following warning:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>CompareIDs.h:15:17 'binary_function&lt;CompareIDs, CompareIDs, bool&gt;' is deprecated<\/strong><\/code><\/pre>\n\n\n\n<p>It refused to compile with C++17.<\/p>\n\n\n\n<p>So, <strong>binary_function<\/strong> is deprecated in C++11, and removed in C++17.<\/p>\n\n\n\n<p style=\"padding-bottom:var(--wp--preset--spacing--20);font-size:clamp(15.747px, 0.984rem + ((1vw - 3.2px) * 0.645), 24px);\"><strong>Why was it deprecated?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Redundancy with C++11 features:<\/strong>&nbsp;With the introduction of features like&nbsp;<strong>decltype<\/strong>&nbsp;and trailing return types in C++11, it became simpler to define the necessary types (<strong>first_argument_type<\/strong>,&nbsp;<strong>second_argument_type<\/strong>,&nbsp;<strong>result_type<\/strong>) directly within your function object class without the need for a base class.<\/li>\n<\/ul>\n\n\n\n<p style=\"padding-bottom:var(--wp--preset--spacing--20);font-size:clamp(15.747px, 0.984rem + ((1vw - 3.2px) * 0.645), 24px);\"><strong>How to replace it?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Define the types directly:<\/strong>&nbsp;If you need to define a binary function object, simply define the required <strong><code>typedef<\/code><\/strong>s within your class:<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Adapters_for_Pointers_to_Functions\"><\/span>Adapters for Pointers to Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><em><strong>Adapters for Pointers to Functions<\/strong><\/em> are provided to allow pointers to unary and binary functions to work with the other function adapters.<\/p>\n\n\n\n<p>Suppose we have two different sets that differ only in their comparison functions:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>set&lt;string, less&lt;string&gt; &gt; set1;\nset&lt;string, greater&lt;string&gt; &gt; set2;<\/strong>\n<\/code><\/pre>\n\n\n\n<p>This can produce problems with the compiler generating multiple copies of much of the code needed to support both of these sets.<\/p>\n\n\n\n<p>Instead, we can use a single instance of <strong>set<\/strong>, by using an adapter for pointers to functions as the type of comparison function to be used to determine the set order:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>set&lt; string, pointer_to_binary_function&lt;const string &amp;, const string &amp;, bool&gt; &gt;<\/strong><\/code><\/pre>\n\n\n\n<p>Here&#8217;s an example of how you could use this functionality:<\/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 Function Adapters\n\/\/\n\/\/  Created by Bryan Higgs on 10\/24\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;set&gt;\n\n\/\/ Use a typedef to simplify types\ntypedef std::set&lt;std::string,\n                std::pointer_to_binary_function\n                    &lt;const std::string &amp;,\n                     const std::string &amp;,\n                     bool&gt; &gt;   SET_TYPE;\n\nbool myLess(const std::string &amp;x, const std::string &amp;y)\n{\n  return x &lt; y;\n}\n\nbool myGreater(const std::string &amp;x, const std::string &amp;y)\n{\n  return x &gt; y;\n}\n\nvoid populateSet(SET_TYPE &amp;s)\n{\n  s.insert(&quot;Have&quot;);\n  s.insert(&quot;you&quot;);\n  s.insert(&quot;hugged&quot;);\n  s.insert(&quot;your&quot;);\n  s.insert(&quot;sweetie&quot;);\n  s.insert(&quot;today&quot;);\n  s.insert(&quot;?&quot;);\n}\n\nint main(int argc, const char * argv[])\n{\n  SET_TYPE set1( ptr_fun(myLess) );\n  populateSet(set1);\n  \n  std::cout &lt;&lt; &quot;set1 (myLess):&quot; &lt;&lt; std::endl;\n  SET_TYPE::iterator i;\n  for (i = set1.begin(); i != set1.end(); i++)\n  {\n    std::cout &lt;&lt; *i &lt;&lt; std::endl;\n  }\n  std::cout &lt;&lt; std::endl;\n\n  std::cout &lt;&lt; &quot;set2 (myGreater):&quot; &lt;&lt; std::endl;\n  SET_TYPE set2( ptr_fun(myGreater) );\n  populateSet(set2);\n  for (i = set2.begin(); i != set2.end(); i++)\n  {\n    std::cout &lt;&lt; *i &lt;&lt; std::endl;\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\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Function Adapters<\/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\/24\/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;string&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;set&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">\/\/ Use a typedef to simplify types<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">typedef<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::set&lt;<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string,<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::pointer_to_binary_function<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                    &lt;<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string &amp;,<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                     <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string &amp;,<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                     <\/span><span style=\"color: #0000FF\">bool<\/span><span style=\"color: #000000\">&gt; &gt;   SET_TYPE;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">bool<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">myLess<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">y<\/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: #AF00DB\">return<\/span><span style=\"color: #000000\"> x &lt; y;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">bool<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">myGreater<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">y<\/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: #AF00DB\">return<\/span><span style=\"color: #000000\"> x &gt; y;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">populateSet<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">SET_TYPE<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">s<\/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: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Have&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;you&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;hugged&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;your&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;sweetie&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;today&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">s<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">insert<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;?&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><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 cbp-line-highlight\"><span style=\"color: #000000\">  SET_TYPE <\/span><span style=\"color: #795E26\">set1<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #795E26\">ptr_fun<\/span><span style=\"color: #000000\">(myLess) );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">populateSet<\/span><span style=\"color: #000000\">(set1);<\/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;set1 (myLess):&quot;<\/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 style=\"color: #267F99\">SET_TYPE<\/span><span style=\"color: #000000\">::iterator i;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (i = <\/span><span style=\"color: #001080\">set1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(); i != <\/span><span style=\"color: #001080\">set1<\/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: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; *i &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\">::cout &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: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;set2 (myGreater):&quot;<\/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 cbp-line-highlight\"><span style=\"color: #000000\">  SET_TYPE <\/span><span style=\"color: #795E26\">set2<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #795E26\">ptr_fun<\/span><span style=\"color: #000000\">(myGreater) );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">populateSet<\/span><span style=\"color: #000000\">(set2);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (i = <\/span><span style=\"color: #001080\">set2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(); i != <\/span><span style=\"color: #001080\">set2<\/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: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; *i &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\">::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:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>set1 (myLess):\n?\nHave\nhugged\nsweetie\ntoday\nyou\nyour\n\nset2 (myGreater):\nyour\nyou\ntoday\nsweetie\nhugged\nHave\n?\n\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:22px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\"><strong>NOTE<\/strong><\/p>\n\n\n\n<p>The above program was compiled with C++11, and received the following warnings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>main.cpp:14:22 'pointer_to_binary_function&lt;const std::string &amp;, const std::string &amp;, bool&gt;' is deprecated\n\nmain.cpp:42:18 'ptr_fun&lt;const std::string &amp;, const std::string &amp;, bool&gt;' is deprecated\n\nmain.cpp:54:18 'ptr_fun&lt;const std::string &amp;, const std::string &amp;, bool&gt;' is deprecated<\/strong><\/code><\/pre>\n\n\n\n<p>It failed to compile using C++17.<\/p>\n\n\n\n<p>So, <strong>pointer_to_binary_function<\/strong> and <strong><code>ptr_fun<\/code><\/strong> are deprecated in C++11, and removed in C++17.<\/p>\n\n\n\n<p class=\"has-text-align-center\">&#8220;You should use modern alternatives like lambdas or&nbsp;<strong>std::function<\/strong>&nbsp;instead.&#8221;<\/p>\n\n\n\n<p><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Categories_of_Generic_Algorithms\"><\/span>Categories of Generic Algorithms<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The STL categorizes its algorithms as follows:<\/p>\n\n\n\n<p><em><strong>Nonmutating Sequence Algorithms<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>for_each, find, find_end, find_first_of, adjacent_find, count, mismatch, equal, search, search_n<\/strong><\/code><\/pre>\n\n\n\n<p><em><strong>Mutating Sequence Algorithms<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>copy, swap, transform, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle, partition<\/strong><\/code><\/pre>\n\n\n\n<p><em><strong>Sorting and Related Operations<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>sort, stable_sort, partial_sort, nth_element, binary_search, lower_bound, upper_bound, equal_range, merge, includes, set_union, set_intersection, set_difference, set_symmetric_difference, push_heap, pop_heap, make_heap, sort_heap, min, max, min_element, max_element, lexicographic_compare, next_permutation, prev_permutation<\/strong>\n<\/code><\/pre>\n\n\n\n<p><em><strong>Generalized Numeric Operations<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>accumulate, inner_product, partial_sum, adjacent_difference<\/strong><\/code><\/pre>\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\/summary-of-the-standard-template-library\/\">Next: Summary of The Standard Template Library<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Function Adapters Just as iterator adapters help you construct a wider variety of iterators, a Function Adapter helps you create a wider variety of function objects. STL provides three kinds of function adapters: WARNING: It appears that a number of these function adapters are now deprecated in C++11, and removed in C++17. There are alternative [&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-2202","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2202","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=2202"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2202\/revisions"}],"predecessor-version":[{"id":2230,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2202\/revisions\/2230"}],"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=2202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}