{"id":566,"date":"2019-08-17T19:02:30","date_gmt":"2019-08-18T02:02:30","guid":{"rendered":"http:\/\/35.243.195.209\/?p=566"},"modified":"2019-08-17T19:02:30","modified_gmt":"2019-08-18T02:02:30","slug":"ways-to-overload-default-order-in-cs-stl","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/08\/17\/ways-to-overload-default-order-in-cs-stl\/","title":{"rendered":"Ways to Overload Default Order in C++&#8217;s STL"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce some methods to overload the default order in C++&#8217;s STL. Please refer to <a href=\"http:\/\/35.243.195.209\/index.php\/2018\/12\/21\/ordered-containers\/\">my other post<\/a> if you are not familiar with Ordered Containers in C++. This post is a helpful reference when solving coding challenges. <\/p>\n<h2>Conclusion<\/h2>\n<p>You are giving a sequence of pairs, <code>{{5, \"five\"}, {1, \"one\"}, {4, \"four\"}, {3, \"three\"}, {2, \"two\"}, {7, \"seven\"}, {6, \"six\"} };<\/code>.<br \/>\nSort them by the integers in decreasing order. Expected order is <code>{{7,&quot;seven&quot;} {6,&quot;six&quot;} {5,&quot;five&quot;} {4,&quot;four&quot;} {3,&quot;three&quot;} {2,&quot;two&quot;} {1,&quot;one&quot;}}<\/code><\/p>\n<h3>Function Pointers<\/h3>\n<p>C++ has function pointers which you can feed into ordered containers&#8217; template. It is very similar to lambda functions. <\/p>\n<pre><code class=\"language-cpp\">bool comp (int a, int b) {\n    return a &gt; b;\n}\nvoid one() {\n    map&lt;int, std::string, decltype(comp)&gt; int2Str(comp);\n}\nvoid another() {\n    bool(*fn_pt)(int, int) = comp; \/\/ fn_pt now is a function pointer with type bool(*)(int, int)\n    map&lt;int, std::string, bool(*)(int, int)&gt; int2Str(fn_pt);\n}<\/code><\/pre>\n<h3>Lambda Functions<\/h3>\n<p>Use lambda functions when you want to do customized sorting or define a global ordered container. For example, <\/p>\n<pre><code class=\"language-cpp\">auto comp = [](int i, int j){return i &gt; j;};\nmap&lt;int, std::string, decltype(comp)&gt; int2Str(comp);<\/code><\/pre>\n<h3>Functors<\/h3>\n<p>A functor is pretty much just a class which defines the <code>operator()<\/code>. That lets you create instances which &quot;look like&quot; a function:<\/p>\n<pre><code class=\"language-cpp\">class Compare {\npublic:\n    bool operator() (int const &amp; a, int const &amp;b) const {\n        return a.key &gt; b.key;\n    }\n};\nmap&lt;int, std::string, Compare&gt; int2Str;\n\/\/ your Compare will be called like  \nCompare comp; \/\/ create an instance\ncomp(a, b)<\/code><\/pre>\n<h3>Customized Class<\/h3>\n<p>You can directly define a class and overloads its comparison function.<\/p>\n<pre><code class=\"language-cpp\">struct KyeValuePair\n{\n    int key;\n    string val;\n\n    Player(int key_, string val_) :\n            key(key_), val(val_)\n    {\n    }\n    bool operator &lt;(const KyeValuePair &amp; anotherInstance) const\n    {\n        return key &gt; anotherInstance.val;\n    }\n};<\/code><\/pre>\n<h2>Reference<\/h2>\n<p><a href=\"http:\/\/fusharblog.com\/3-ways-to-define-comparison-functions-in-cpp\/\">This blog<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce some methods to overload the default order in C++&#8217;s STL. Please refer to my other post if you are not familiar with Ordered Containers in C++. This post is a helpful reference when solving coding challenges. Conclusion You are giving a sequence of pairs, {{5, &#8220;five&#8221;}, {1, &#8220;one&#8221;},&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-566","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":2,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/566\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/566\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}