
{"id":598,"date":"2014-11-15T23:33:23","date_gmt":"2014-11-15T23:33:23","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=598"},"modified":"2014-11-15T23:40:17","modified_gmt":"2014-11-15T23:40:17","slug":"three-easy-steps-to-modernize-your-c-algorithms","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=598","title":{"rendered":"Three easy steps to modernize your C++ algorithms"},"content":{"rendered":"<p>Algorithms are used for calculation, data processing, and automated reasoning. Programming them\u00a0is not always an easy task and it depends on their complexity.\u00a0In C++ many efforts was done to simplify their implementation and to make them more powerful. Indeed, during the last ten years many C++ experts promotes the use of &#8220;Modern C++ Design&#8221; to improve the quality of the\u00a0C++ project design and\u00a0implementation.<\/p>\n<p>In this post we will discover three steps to modernize a C++ algorithm, for that we take as example the quick sort algorithm. Here&#8217;s a classic\u00a0implementation:<!--more--><\/p>\n<pre class=\"prettyprint prettyprinted\" style=\"color: #333333;\"><code style=\"color: inherit;\"><span class=\"com\" style=\"color: #880000;\">\/\/ The partition function\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pln\" style=\"color: #000000;\"> partition<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pun\" style=\"color: #666600;\">*<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pln\" style=\"color: #000000;\"> p<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pln\" style=\"color: #000000;\"> r<\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pun\" style=\"color: #666600;\">{\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">        int<\/span><span class=\"pln\" style=\"color: #000000;\"> pivot <\/span><span class=\"pun\" style=\"color: #666600;\">=<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">r<\/span><span class=\"pun\" style=\"color: #666600;\">];\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">        while<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\"> p <\/span><span class=\"pun\" style=\"color: #666600;\">&lt;<\/span><span class=\"pln\" style=\"color: #000000;\"> r <\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pun\" style=\"color: #666600;\">{\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">                 while<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">p<\/span><span class=\"pun\" style=\"color: #666600;\">]<\/span><span class=\"pun\" style=\"color: #666600;\">&lt;<\/span><span class=\"pln\" style=\"color: #000000;\"> pivot <\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n                     p<\/span><span class=\"pun\" style=\"color: #666600;\">++;\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">                 while<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">r<\/span><span class=\"pun\" style=\"color: #666600;\">]<\/span><span class=\"pun\" style=\"color: #666600;\">&gt;<\/span><span class=\"pln\" style=\"color: #000000;\"> pivot <\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n                    r<\/span><span class=\"pun\" style=\"color: #666600;\">--;\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">                if<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">p<\/span><span class=\"pun\" style=\"color: #666600;\">]<\/span><span class=\"pun\" style=\"color: #666600;\">==<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">r<\/span><span class=\"pun\" style=\"color: #666600;\">]<\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n                    p<\/span><span class=\"pun\" style=\"color: #666600;\">++;\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">                else<\/span><span class=\"kwd\" style=\"color: #000088;\">if<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\"> p <\/span><span class=\"pun\" style=\"color: #666600;\">&lt;<\/span><span class=\"pln\" style=\"color: #000000;\"> r <\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pun\" style=\"color: #666600;\">{\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">                     int<\/span><span class=\"pln\" style=\"color: #000000;\"> tmp <\/span><span class=\"pun\" style=\"color: #666600;\">=<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">p<\/span><span class=\"pun\" style=\"color: #666600;\">];<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n                     input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">p<\/span><span class=\"pun\" style=\"color: #666600;\">]<\/span><span class=\"pun\" style=\"color: #666600;\">=<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">r<\/span><span class=\"pun\" style=\"color: #666600;\">];<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n                     input<\/span><span class=\"pun\" style=\"color: #666600;\">[<\/span><span class=\"pln\" style=\"color: #000000;\">r<\/span><span class=\"pun\" style=\"color: #666600;\">]<\/span><span class=\"pun\" style=\"color: #666600;\">=<\/span><span class=\"pln\" style=\"color: #000000;\"> tmp<\/span><span class=\"pun\" style=\"color: #666600;\">;\r\n<\/span><span class=\"pun\" style=\"color: #666600;\">                }\r\n<\/span><span class=\"pun\" style=\"color: #666600;\">        }\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">         return<\/span><span class=\"pln\" style=\"color: #000000;\"> r<\/span><span class=\"pun\" style=\"color: #666600;\">;\r\n<\/span><span class=\"pun\" style=\"color: #666600;\">}\r\n<\/span><span class=\"com\" style=\"color: #880000;\">\/\/ The quicksort recursive function\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">void<\/span><span class=\"pln\" style=\"color: #000000;\"> quicksort<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pun\" style=\"color: #666600;\">*<\/span><span class=\"pln\" style=\"color: #000000;\"> input<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pln\" style=\"color: #000000;\"> p<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"kwd\" style=\"color: #000088;\">int<\/span><span class=\"pln\" style=\"color: #000000;\"> r<\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pun\" style=\"color: #666600;\">{\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">        if<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\"> p <\/span><span class=\"pun\" style=\"color: #666600;\">&lt;<\/span><span class=\"pln\" style=\"color: #000000;\"> r <\/span><span class=\"pun\" style=\"color: #666600;\">)<\/span><span class=\"pun\" style=\"color: #666600;\">{\r\n<\/span><span class=\"kwd\" style=\"color: #000088;\">              int<\/span><span class=\"pln\" style=\"color: #000000;\"> j <\/span><span class=\"pun\" style=\"color: #666600;\">=<\/span><span class=\"pln\" style=\"color: #000000;\"> partition<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\">input<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"pln\" style=\"color: #000000;\"> p<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"pln\" style=\"color: #000000;\"> r<\/span><span class=\"pun\" style=\"color: #666600;\">);<\/span><span class=\"pln\" style=\"color: #000000;\">        \r\n              quicksort<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\">input<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"pln\" style=\"color: #000000;\"> p<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"pln\" style=\"color: #000000;\"> j<\/span><span class=\"pun\" style=\"color: #666600;\">-<\/span><span class=\"lit\" style=\"color: #006666;\">1<\/span><span class=\"pun\" style=\"color: #666600;\">);<\/span><span class=\"pln\" style=\"color: #000000;\">\r\n              quicksort<\/span><span class=\"pun\" style=\"color: #666600;\">(<\/span><span class=\"pln\" style=\"color: #000000;\">input<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"pln\" style=\"color: #000000;\"> j<\/span><span class=\"pun\" style=\"color: #666600;\">+<\/span><span class=\"lit\" style=\"color: #006666;\">1<\/span><span class=\"pun\" style=\"color: #666600;\">,<\/span><span class=\"pln\" style=\"color: #000000;\"> r<\/span><span class=\"pun\" style=\"color: #666600;\">);\r\n<\/span><span class=\"pun\" style=\"color: #666600;\">        }\r\n<\/span><span class=\"pun\" style=\"color: #666600;\">}<\/span><\/code><\/pre>\n<p><code style=\"color: inherit;\"><span class=\"pun\" style=\"color: #666600;\">\u00a0<\/span><\/code>After all, \u00a0what the common traits of algorithms?<\/p>\n<ul>\n<li>Using containers of \u00a0an element kind and Iterating over\u00a0them.<\/li>\n<li>Comparison between elements.<\/li>\n<li>And of course some \u00a0treatments over elements.<\/li>\n<\/ul>\n<p>In our implementation the container is a raw array of int, we iterate thought increment and decrement. We compare using &#8220;&lt;&#8221; and &#8220;&gt;&#8221;, and we have some traitements like swaping data.<\/p>\n<p>Let&#8217;s try to improve each one of these traits:<\/p>\n<p><strong>Step1: Replace containers by iterators<\/strong><\/p>\n<p>Using not generic containers will force us to use a specific element kind.\u00a0To apply the same algorithm to other types, we have to copy\/paste the code. The generic containers resolve this issue, and permit to use any element kind, for example for our quick sort algorithm, we can use std::vector&lt;T&gt; as container instead of a raw array.<\/p>\n<p>A raw array or an std::vector \u00a0is just one possibility between many others to represent a set of elements, we can also apply the same algorithm to\u00a0a linked list, a queue or whatever any other container. For this needs the iterator is the best choice to abstract the container used.<\/p>\n<p>An\u00a0iterator\u00a0is any object that, pointing to some element in a range of elements , has the ability to iterate through the elements of that range using a set of operators (with at least the increment (++) and dereference (*) operators).\u00a0Iterators are classified into five categories depending on the functionality they implement: Input, Output, Forward, Bidirectional and random access.<\/p>\n<p>In our algorithm\u00a0we have to specify which kind of iterator to use. For that we have to detect \u00a0which iterations are used.\u00a0For the quick sort algo the \u00a0increment and \u00a0decrement iterations are applied. Therefore, a bidirectional iterator is needed.\u00a0Using iterators we can \u00a0define the method like this:<\/p>\n<pre class=\"de1\"><span class=\"kw2\" style=\"color: #0000ff;\">template<\/span><span class=\"sy1\" style=\"color: #000080;\">&lt;<\/span> <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> BidirectionalIterator <span class=\"sy1\" style=\"color: #000080;\">&gt;<\/span>\r\n<span class=\"kw4\" style=\"color: #0000ff;\">void<\/span> quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> BidirectionalIterator first, BidirectionalIterator last <span class=\"br0\" style=\"color: #008000;\">)<\/span><\/pre>\n<p><strong>Step2: Make the comparator generic if possible<\/strong><\/p>\n<p>For some algorithms, the elements treated are not only numbers, they could be string or a class. In this case make the comparator generic will permit us to have a more generic algorithm.<\/p>\n<p>The quick sort algo could also be applied to a list of strings, Therefore it&#8217;s better to have a generic comparator.<\/p>\n<p>After using a\u00a0generic comparator, the definition could be modified like this:<\/p>\n<pre class=\"de1\"><span class=\"kw2\" style=\"color: #0000ff;\">template<\/span><span class=\"sy1\" style=\"color: #000080;\">&lt;<\/span> <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> BidirectionalIterator, <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> Compare <span class=\"sy1\" style=\"color: #000080;\">&gt;<\/span>\r\n<span class=\"kw4\" style=\"color: #0000ff;\">void<\/span> quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> BidirectionalIterator first, BidirectionalIterator last, Compare cmp <span class=\"br0\" style=\"color: #008000;\">)<\/span><\/pre>\n<p><strong>Step3: Replace treatments by standard ones<\/strong><\/p>\n<p>Most algorithms use recurrent traitements like min, max and swap. for these operations it&#8217;s preferable to not reinvent the wheel, and uses the standard implementation existing in the &lt;algorithm&gt; header.<\/p>\n<p>In our case we can use the swap method from the STL than creating our specific method.<\/p>\n<pre class=\"de1\">std<span class=\"sy4\" style=\"color: #008080;\">::<\/span><span class=\"me2\" style=\"color: #007788;\">iter_swap<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> pivot, left <span class=\"br0\" style=\"color: #008000;\">)<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span><\/pre>\n<p>And here&#8217;s the modified result after these three steps:<\/p>\n<pre class=\"de1\"><span class=\"co2\" style=\"color: #339900;\">#include &lt;functional&gt;<\/span>\r\n<span class=\"co2\" style=\"color: #339900;\">#include &lt;algorithm&gt;<\/span>\r\n<span class=\"co2\" style=\"color: #339900;\">#include &lt;iterator&gt;<\/span>\r\n \r\n<span class=\"kw2\" style=\"color: #0000ff;\">template<\/span><span class=\"sy1\" style=\"color: #000080;\">&lt;<\/span> <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> BidirectionalIterator, <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> Compare <span class=\"sy1\" style=\"color: #000080;\">&gt;<\/span>\r\n<span class=\"kw4\" style=\"color: #0000ff;\">void<\/span> quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> BidirectionalIterator first, BidirectionalIterator last, Compare cmp <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">{<\/span>\r\n    <span class=\"kw1\" style=\"color: #0000ff;\">if<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> first <span class=\"sy3\" style=\"color: #000040;\">!<\/span><span class=\"sy1\" style=\"color: #000080;\">=<\/span> last <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">{<\/span>\r\n        BidirectionalIterator left  <span class=\"sy1\" style=\"color: #000080;\">=<\/span> first<span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n        BidirectionalIterator right <span class=\"sy1\" style=\"color: #000080;\">=<\/span> last<span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n        BidirectionalIterator pivot <span class=\"sy1\" style=\"color: #000080;\">=<\/span> left<span class=\"sy2\" style=\"color: #000040;\">++<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n \r\n        <span class=\"kw1\" style=\"color: #0000ff;\">while<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> left <span class=\"sy3\" style=\"color: #000040;\">!<\/span><span class=\"sy1\" style=\"color: #000080;\">=<\/span> right <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">{<\/span>\r\n            <span class=\"kw1\" style=\"color: #0000ff;\">if<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> cmp<span class=\"br0\" style=\"color: #008000;\">(<\/span> <span class=\"sy2\" style=\"color: #000040;\">*<\/span>left, <span class=\"sy2\" style=\"color: #000040;\">*<\/span>pivot <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">{<\/span>\r\n                <span class=\"sy2\" style=\"color: #000040;\">++<\/span>left<span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n            <span class=\"br0\" style=\"color: #008000;\">}<\/span> <span class=\"kw1\" style=\"color: #0000ff;\">else<\/span> <span class=\"br0\" style=\"color: #008000;\">{<\/span>\r\n                <span class=\"kw1\" style=\"color: #0000ff;\">while<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> <span class=\"br0\" style=\"color: #008000;\">(<\/span>left <span class=\"sy3\" style=\"color: #000040;\">!<\/span><span class=\"sy1\" style=\"color: #000080;\">=<\/span> right<span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"sy3\" style=\"color: #000040;\">&amp;&amp;<\/span> cmp<span class=\"br0\" style=\"color: #008000;\">(<\/span> <span class=\"sy2\" style=\"color: #000040;\">*<\/span>pivot, <span class=\"sy2\" style=\"color: #000040;\">*<\/span>right <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">)<\/span>\r\n                    <span class=\"sy2\" style=\"color: #000040;\">--<\/span>right<span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n                std<span class=\"sy4\" style=\"color: #008080;\">::<\/span><span class=\"me2\" style=\"color: #007788;\">iter_swap<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> left, right <span class=\"br0\" style=\"color: #008000;\">)<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n            <span class=\"br0\" style=\"color: #008000;\">}<\/span>\r\n        <span class=\"br0\" style=\"color: #008000;\">}<\/span>\r\n \r\n        <span class=\"sy2\" style=\"color: #000040;\">--<\/span>left<span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n        std<span class=\"sy4\" style=\"color: #008080;\">::<\/span><span class=\"me2\" style=\"color: #007788;\">iter_swap<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span> pivot, left <span class=\"br0\" style=\"color: #008000;\">)<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n \r\n        quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> first, left, cmp <span class=\"br0\" style=\"color: #008000;\">)<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n        quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> right, last, cmp <span class=\"br0\" style=\"color: #008000;\">)<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n    <span class=\"br0\" style=\"color: #008000;\">}<\/span>\r\n<span class=\"br0\" style=\"color: #008000;\">}<\/span>\r\n \r\n<span class=\"kw2\" style=\"color: #0000ff;\">template<\/span><span class=\"sy1\" style=\"color: #000080;\">&lt;<\/span> <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> BidirectionalIterator <span class=\"sy1\" style=\"color: #000080;\">&gt;<\/span>\r\n    <span class=\"kw2\" style=\"color: #0000ff;\">inline<\/span> <span class=\"kw4\" style=\"color: #0000ff;\">void<\/span> quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> BidirectionalIterator first, BidirectionalIterator last <span class=\"br0\" style=\"color: #008000;\">)<\/span> <span class=\"br0\" style=\"color: #008000;\">{<\/span>\r\n        quick_sort<span class=\"br0\" style=\"color: #008000;\">(<\/span> first, last,\r\n                std<span class=\"sy4\" style=\"color: #008080;\">::<\/span><span class=\"me2\" style=\"color: #007788;\">less_equal<\/span><span class=\"sy1\" style=\"color: #000080;\">&lt;<\/span> <span class=\"kw2\" style=\"color: #0000ff;\">typename<\/span> std<span class=\"sy4\" style=\"color: #008080;\">::<\/span><span class=\"me2\" style=\"color: #007788;\">iterator_traits<\/span><span class=\"sy1\" style=\"color: #000080;\">&lt;<\/span> BidirectionalIterator <span class=\"sy1\" style=\"color: #000080;\">&gt;<\/span><span class=\"sy4\" style=\"color: #008080;\">::<\/span><span class=\"me2\" style=\"color: #007788;\">value_type<\/span> <span class=\"sy1\" style=\"color: #000080;\">&gt;<\/span><span class=\"br0\" style=\"color: #008000;\">(<\/span><span class=\"br0\" style=\"color: #008000;\">)<\/span>\r\n                <span class=\"br0\" style=\"color: #008000;\">)<\/span><span class=\"sy4\" style=\"color: #008080;\">;<\/span>\r\n    <span class=\"br0\" style=\"color: #008000;\">}<\/span><\/pre>\n<p>This implementation has the following advantages:<\/p>\n<ul>\n<li>Could be applied to many element kind.<\/li>\n<li>The container could be vector, set, list or whatever container with a biderectional iterator.<\/li>\n<li>It uses a well optimized and tested standard functions.<\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><\/p>\n<p>The Modern C++ design brings many advantages. Moreover no advanced techniques are needed to modernize a C++ algorithm, and it&#8217;s worth to change their implementations to use the Modern C++ recommendations.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Algorithms are used for calculation, data processing, and automated reasoning. Programming them\u00a0is not always an easy task and it depends on their complexity.\u00a0In C++ many efforts was done to simplify their implementation and to make them more powerful. Indeed, during the last ten years many C++ experts promotes the use of &#8220;Modern C++ Design&#8221; to &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=598\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Three easy steps to modernize your C++ algorithms&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=598"}],"version-history":[{"count":19,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":765,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/765"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}