{"id":651,"date":"2020-03-07T15:27:50","date_gmt":"2020-03-07T07:27:50","guid":{"rendered":"http:\/\/kylelv.com\/?p=651"},"modified":"2020-03-10T00:01:19","modified_gmt":"2020-03-09T16:01:19","slug":"%e7%bb%a7%e6%89%bf%e8%87%aastring%e7%9a%84mystring","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=651","title":{"rendered":"\u7ee7\u627f\u81eastring\u7684MyString"},"content":{"rendered":"\n<h2 class=\"has-text-align-center wp-block-heading\">\u7ee7\u627f\u81eastring\u7684MyString<\/h2>\n\n\n\n<p class=\"has-text-align-center\">\u603b\u65f6\u95f4\u9650\u5236:\u00a01000ms\u00a0\u5185\u5b58\u9650\u5236:\u00a065536kB<\/p>\n\n\n\n<p class=\"has-text-align-left\">\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"has-text-align-left\">\u7a0b\u5e8f\u586b\u7a7a\uff0c\u8f93\u51fa\u6307\u5b9a\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;cstdlib&gt;\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\nclass MyString:public string\n{<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u5728\u6b64\u5904\u8865\u5145\u4f60\u7684\u4ee3\u7801<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">};\n\n\nint main()\n{\n\tMyString s1(\"abcd-\"),s2,s3(\"efgh-\"),s4(s1);\n\tMyString SArray[4] = {\"big\",\"me\",\"about\",\"take\"};\n\tcout &lt;&lt; \"1. \" &lt;&lt; s1 &lt;&lt; s2 &lt;&lt; s3&lt;&lt; s4&lt;&lt; endl;\n\ts4 = s3;\n\ts3 = s1 + s3;\n\tcout &lt;&lt; \"2. \" &lt;&lt; s1 &lt;&lt; endl;\n\tcout &lt;&lt; \"3. \" &lt;&lt; s2 &lt;&lt; endl;\n\tcout &lt;&lt; \"4. \" &lt;&lt; s3 &lt;&lt; endl;\n\tcout &lt;&lt; \"5. \" &lt;&lt; s4 &lt;&lt; endl;\n\tcout &lt;&lt; \"6. \" &lt;&lt; s1[2] &lt;&lt; endl;\n\ts2 = s1;\n\ts1 = \"ijkl-\";\n\ts1[2] = 'A' ;\n\tcout &lt;&lt; \"7. \" &lt;&lt; s2 &lt;&lt; endl;\n\tcout &lt;&lt; \"8. \" &lt;&lt; s1 &lt;&lt; endl;\n\ts1 += \"mnop\";\n\tcout &lt;&lt; \"9. \" &lt;&lt; s1 &lt;&lt; endl;\n\ts4 = \"qrst-\" + s2;\n\tcout &lt;&lt; \"10. \" &lt;&lt; s4 &lt;&lt; endl;\n\ts1 = s2 + s4 + \" uvw \" + \"xyz\";\n\tcout &lt;&lt; \"11. \" &lt;&lt; s1 &lt;&lt; endl;\n        sort(SArray,SArray+4);\n\tfor( int i = 0;i &lt; 4;i ++ )\n\tcout &lt;&lt; SArray[i] &lt;&lt; endl;\n\t\/\/s1\u7684\u4ece\u4e0b\u68070\u5f00\u59cb\u957f\u5ea6\u4e3a4\u7684\u5b50\u4e32\n\tcout &lt;&lt; s1(0,4) &lt;&lt; endl;\n\t\/\/s1\u7684\u4ece\u4e0b\u68075\u5f00\u59cb\u957f\u5ea6\u4e3a10\u7684\u5b50\u4e32\n\tcout &lt;&lt; s1(5,10) &lt;&lt; endl;\n\treturn 0;\n}<\/pre>\n\n\n\n<p>\u8f93\u5165<\/p>\n\n\n\n<p>\u65e0<\/p>\n\n\n\n<p>\u8f93\u51fa<\/p>\n\n\n\n<p>1. abcd-efgh-abcd-<br>2. abcd-<br>3.<br>4. abcd-efgh-<br>5. efgh-<br>6. c<br>7. abcd-<br>8. ijAl-<br>9. ijAl-mnop<br>10. qrst-abcd-<br>11. abcd-qrst-abcd- uvw xyz<br>about<br>big<br>me<br>take<br>abcd<br>qrst-abcd-<\/p>\n\n\n\n<p>\u6837\u4f8b\u8f93\u5165<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u65e0<\/pre>\n\n\n\n<p>\u6837\u4f8b\u8f93\u51fa<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1. abcd-efgh-abcd-\n2. abcd-\n3. \n4. abcd-efgh-\n5. efgh-\n6. c\n7. abcd-\n8. ijAl-\n9. ijAl-mnop\n10. qrst-abcd-\n11. abcd-qrst-abcd- uvw xyz\nabout\nbig\nme\ntake\nabcd\nqrst-abcd-\n<\/pre>\n\n\n\n<p>\u63d0\u793a\u63d0\u793a 1\uff1a\u5982\u679c\u5c06\u7a0b\u5e8f\u4e2d\u6240\u6709 &#8220;MyString&#8221; \u7528 &#8220;string&#8221; \u66ff\u6362\uff0c\u90a3\u4e48\u9664<br>\u4e86\u6700\u540e\u4e24\u6761\u7ea2\u8272\u7684\u8bed\u53e5\u7f16\u8bd1\u65e0\u6cd5\u901a\u8fc7\u5916\uff0c\u5176\u4ed6\u8bed\u53e5\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u800c\u4e14\u8f93\u51fa\u548c\u524d<br>\u9762\u7ed9\u7684\u7ed3\u679c\u543b\u5408\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cMyString \u7c7b\u5bf9 string \u7c7b\u7684\u529f\u80fd\u6269\u5145\u53ea\u4f53\u73b0\u5728\u6700<br>\u540e\u4e24\u6761\u8bed\u53e5\u4e0a\u9762\u3002<\/p>\n\n\n\n<p>\u63d0\u793a 2: string \u7c7b\u6709\u4e00\u4e2a\u6210\u5458\u51fd\u6570 string substr(int start,int<br>length); \u80fd\u591f\u6c42\u4ece start \u4f4d\u7f6e\u5f00\u59cb\uff0c\u957f\u5ea6\u4e3a length \u7684\u5b50\u4e32<\/p>\n\n\n\n<p>\u63d0\u793a 3: C++\u4e2d\uff0c\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u53ef\u4ee5\u8d4b\u503c\u7ed9\u57fa\u7c7b\u5bf9\u8c61\uff0c\u56e0\u4e3a\uff0c\u4e00\u4e2a\u6d3e\u751f<br>\u7c7b\u5bf9\u8c61\uff0c\u4e5f\u53ef\u770b\u4f5c\u662f\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\uff08\u5927\u5b66\u751f\u662f\u5b66\u751f\uff09\u3002\u53cd\u8fc7\u6765\u5219\u4e0d\u884c(\u5b66\u751f\u672a<br>\u5fc5\u662f\u5927\u5b66\u751f\uff09 \u540c\u6837\uff0c\u8c03\u7528\u9700\u8981\u57fa\u7c7b\u5bf9\u8c61\u4f5c\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u4ee5\u6d3e\u751f\u7c7b\u5bf9\u8c61\u4f5c\u4e3a\u5b9e\u53c2\uff0c\u4e5f\u662f\u6ca1\u6709\u95ee\u9898\u7684<\/p>\n\n\n\n<p>\u6765\u6e90Guo Wei<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;cstdlib>\n#include &lt;iostream>\n#include &lt;string>\n#include &lt;algorithm>\nusing namespace std;\nclass MyString:public string\n{\n\/\/ \u5728\u6b64\u5904\u8865\u5145\u4f60\u7684\u4ee3\u7801\npublic:\n\tMyString():string(\"\\0\"){}\n\tMyString(const char *s):string(s){}\n\tMyString(const MyString &amp;s):string(s){}\n\tMyString operator +(const char *s){\n\t\tstring a=*this;\n\t\treturn MyString((a+s).c_str());\n\t}\n\tMyString operator +(const MyString &amp;s){\n\t\tstring a=*this,b=s;\n\t\treturn MyString((a+b).c_str());\n\t}\n\tfriend MyString operator +(const char *s,const MyString &amp;z){\n\t\tstring a=s,b=z;\n\t\treturn MyString((a+b).c_str());\n\t}\n\tstring operator ()(int x,int y){\n\t\tstring tp;\n\t\tfor(int i=0;i&lt;y;i++) tp+=(*this)[i+x];\n\t\treturn tp;\n\t}\n};\n\n\nint main()\n{\n\tMyString s1(\"abcd-\"),s2,s3(\"efgh-\"),s4(s1);\n\tMyString SArray[4] = {\"big\",\"me\",\"about\",\"take\"};\n\tcout &lt;&lt; \"1. \" &lt;&lt; s1 &lt;&lt; s2 &lt;&lt; s3&lt;&lt; s4&lt;&lt; endl;\n\ts4 = s3;\n\ts3 = s1 + s3;\n\tcout &lt;&lt; \"2. \" &lt;&lt; s1 &lt;&lt; endl;\n\tcout &lt;&lt; \"3. \" &lt;&lt; s2 &lt;&lt; endl;\n\tcout &lt;&lt; \"4. \" &lt;&lt; s3 &lt;&lt; endl;\n\tcout &lt;&lt; \"5. \" &lt;&lt; s4 &lt;&lt; endl;\n\tcout &lt;&lt; \"6. \" &lt;&lt; s1[2] &lt;&lt; endl;\n\ts2 = s1;\n\ts1 = \"ijkl-\";\n\ts1[2] = 'A' ;\n\tcout &lt;&lt; \"7. \" &lt;&lt; s2 &lt;&lt; endl;\n\tcout &lt;&lt; \"8. \" &lt;&lt; s1 &lt;&lt; endl;\n\ts1 += \"mnop\";\n\tcout &lt;&lt; \"9. \" &lt;&lt; s1 &lt;&lt; endl;\n\ts4 = \"qrst-\" + s2;\n\tcout &lt;&lt; \"10. \" &lt;&lt; s4 &lt;&lt; endl;\n\ts1 = s2 + s4 + \" uvw \" + \"xyz\";\n\tcout &lt;&lt; \"11. \" &lt;&lt; s1 &lt;&lt; endl;\n        sort(SArray,SArray+4);\n\tfor( int i = 0;i &lt; 4;i ++ )\n\tcout &lt;&lt; SArray[i] &lt;&lt; endl;\n\t\/\/s1\u7684\u4ece\u4e0b\u68070\u5f00\u59cb\u957f\u5ea6\u4e3a4\u7684\u5b50\u4e32\n\tcout &lt;&lt; s1(0,4) &lt;&lt; endl;\n\t\/\/s1\u7684\u4ece\u4e0b\u68075\u5f00\u59cb\u957f\u5ea6\u4e3a10\u7684\u5b50\u4e32\n\tcout &lt;&lt; s1(5,10) &lt;&lt; endl;\n\treturn 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ee7\u627f\u81eastring\u7684MyString \u603b\u65f6\u95f4\u9650\u5236:\u00a01000ms\u00a0\u5185\u5b58\u9650\u5236:\u00a065536kB \u63cf\u8ff0 \u7a0b\u5e8f\u586b\u7a7a\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-651","post","type-post","status-publish","format-standard","hentry","category-70"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=651"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":661,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/651\/revisions\/661"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}