{"id":83,"date":"2012-04-23T13:29:21","date_gmt":"2012-04-23T05:29:21","guid":{"rendered":"http:\/\/fishinbox.tk\/?p=83"},"modified":"2012-04-23T13:29:21","modified_gmt":"2012-04-23T05:29:21","slug":"overload-logical-operators-sort-strings-alphabetically","status":"publish","type":"post","link":"https:\/\/zhuangzr.me\/wordpress\/2012\/04\/overload-logical-operators-sort-strings-alphabetically\/","title":{"rendered":"Overload the Logical Operators to Sort Strings Alphabetically"},"content":{"rendered":"<h1>Overload the Logical Operators to Sort Strings Alphabetically<\/h1>\n<p>In c++, we have function like strcmp(const char* c1,const char* c2) to help us decide which is alphabetically before the other. Yet we still want to use a more easy-going function as a bool operator &lt; or something else like other logical operators.<\/p>\n<p>And here we have a customized user-define class String, which is incomplete due this is only for demonstration of overloading operators.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\n#include &lt;iomanip&gt;\nusing namespace std;\n\nclass String{\n\n\tchar* chars;\n\npublic:\n\n\tint length;\n\n\tString String::operator =(const char* r){\n\t\tlength=strlen(r);\n\t\tchars=(char*)malloc((length+1)*sizeof(char));\n\t\tfor(int i=0;i&lt;=length;i++){\n\t\t\tthis-&gt;chars&#x5B;i]=r&#x5B;i];\n\t\t}\n\t\treturn *this;\n\t}\n\n\tString String::operator =(String r){\n\t\tlength=r.length;\n\t\tchars=(char*)malloc((length+1)*sizeof(char));\n\t\tfor(int i=0;i&lt;=length;i++){\n\t\t\tthis-&gt;chars&#x5B;i]=r&#x5B;i];\n\t\t}\n\t\treturn *this;\n\t}\n\n\tchar &amp;String::operator &#x5B;](int idx){\n\t\treturn chars&#x5B;idx];\n\t}\n\n\tbool operator &lt;(String r)\n\t{\n\t\tfor(int idx=0;idx&lt;=(length&lt;r.length?length:r.length);idx++)\n\t\t{\n\t\t\tif(tolower(chars&#x5B;idx])&lt;tolower(r&#x5B;idx]))\n\t\t\t\treturn true;\n\t\t\telse\n\t\t\t\tif(tolower(chars&#x5B;idx])&gt;tolower(r&#x5B;idx]))\n\t\t\t\t\treturn false;\n\t\t\t\telse\n\t\t\t\t\tcontinue;\n\t\t}\n\t\treturn false;\n\t}\n\n\tbool operator &gt;(String r)\n\t{\n\t\treturn r&lt;*this;\n\t}\n\n\tbool operator ==(String r)\n\t{\n\t\treturn !(*this&gt;r)&amp;&amp;!(*this&lt;r);\n\t}\n\n\tbool operator !=(String r)\n\t{\n\t\treturn !(*this==r);\n\t}\n\n\tbool operator &lt;=(String r)\n\t{\n\t\treturn !(*this&gt;r);\n\t}\n\n\tbool operator &gt;=(String r)\n\t{\n\t\treturn !(*this&lt;r);\n\t}\n\n\tfriend ostream&amp;operator &lt;&lt;(ostream &amp;os,String &amp;right);\n\n\tfriend istream&amp;operator &lt;&lt;(istream &amp;is,String &amp;right);\n\n\tString(int n){\n\t\tlength=n;\n\t\tchars=new char&#x5B;length];\n\t}\n\n\tString (){\n\t\tlength=0;\n\t}\n\n\tString (const char* cs){\n\t\t*this=cs;\n\t}\n\n};\n\nostream &amp; operator&lt;&lt;(ostream &amp;os,  String &amp;right)\n{\n\tfor(int i=0;i&lt;=right.length;i++)\n\t{\n\t\tos&lt;&lt;right&#x5B;i];\n\t}\n\n\treturn os;\n}\n\nistream &amp; operator&gt;&gt;(istream &amp;is,String &amp;right)\n{\n\tint idx=2;\n\tchar* tmpcs=new char&#x5B;idx];\n\tchar c;\n\tint i=0;\n\tfor(;;i++)\n\t{\n\t\tc=is.get();\n\t\tif(c!='n')\n\t\t{\n\t\t\tif(i&gt;=idx)\n\t\t\t{\n\t\t\t\tidx=i+1;\n\t\t\t\ttmpcs=(char*)realloc(tmpcs,idx*sizeof(char));\n\t\t\t}\n\t\t\tif(isalpha(c))\n\t\t\t{\n\t\t\t\ttmpcs&#x5B;i]=c;\n\t\t\t}\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif(i&gt;=idx)\n\t\t\t{\n\t\t\t\tidx=i+1;\n\t\t\t\ttmpcs=(char*)realloc(tmpcs,idx*sizeof(char));\n\t\t\t}\n\t\t\ttmpcs&#x5B;i]='';\n\t\t\tbreak;\n\t\t}\n\t}\n\tright=tmpcs;\n\treturn is;\n\n}\n\ntemplate &lt;typename T&gt;\nvoid sort(T *source,int startidx,int endidx)\n{\n\tfor(int i=startidx+1;i&lt;=endidx;i++)\n\t{\n\t\tfor(int j=startidx;j&lt;i;j++)\n\t\t{\n\t\t\tif(source&#x5B;i]&lt;source&#x5B;j])\n\t\t\t{\n\t\t\t\tT key=source&#x5B;i];\n\t\t\t\tfor(int k=i;k&gt;j;k--)\n\t\t\t\t{\n\t\t\t\t\tsource&#x5B;k]=source&#x5B;k-1];\n\t\t\t\t}\n\t\t\t\tsource&#x5B;j]=key;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\nint main()\n{\n\tString group&#x5B;5];\n\n\tcout&lt;&lt;&quot;Please input 5 words&quot;&lt;&lt;endl;\n\tfor(int i=0;i&lt;5;i++)\n\t{\n\t\tcin&gt;&gt;group&#x5B;i];\n\t}\n\tsort(group,0,4);\n\tcout&lt;&lt;&quot;after the sorting&quot;&lt;&lt;endl;\n\tfor(int i=0;i&lt;5;i++)\n\t{\n\t\tcout&lt;&lt;group&#x5B;i]&lt;&lt;endl;\n\t}\n\tsystem(&quot;pause&quot;);\n\treturn 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overload the Logical Operators to Sort Strings Alphabetically In c++, we have function like strcmp(const char* c1,const char* c2) to help us decide which is alphabetically before the other. Yet we still want to use a more easy-going function as a bool operator &lt; or something else like other logical operators. And here we have &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zhuangzr.me\/wordpress\/2012\/04\/overload-logical-operators-sort-strings-alphabetically\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Overload the Logical Operators to Sort Strings Alphabetically&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[23,25,28,38,43,63],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-cpp-programming","tag-alphabetically","tag-c","tag-cpp","tag-logical-operators","tag-overload","tag-programming"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":0,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}