{"id":43,"date":"2021-08-26T17:20:35","date_gmt":"2021-08-26T09:20:35","guid":{"rendered":"http:\/\/www.gislxz.top\/?p=43"},"modified":"2022-03-30T20:00:39","modified_gmt":"2022-03-30T12:00:39","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%ef%bc%881%ef%bc%89-%e7%ba%bf%e6%80%a7%e8%a1%a8-%e6%95%b0%e7%bb%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.gislxz.com\/index.php\/2021\/08\/26\/%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%ef%bc%881%ef%bc%89-%e7%ba%bf%e6%80%a7%e8%a1%a8-%e6%95%b0%e7%bb%84%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"\u6570\u636e\u7ed3\u6784\uff081\uff09 \u7ebf\u6027\u8868-\u6570\u7ec4\u5b9e\u73b0"},"content":{"rendered":"\n<p> \u7ebf\u6027\u8868\u662f\u6700\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e00\u822c\u6709\u6570\u7ec4\u5b9e\u73b0\u548c\u94fe\u5f0f\u63cf\u8ff0\u4e24\u79cd\uff0c\u6570\u7ec4\u5b9e\u73b0\u53ef\u4ee5\u7528\u57fa\u7840\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u7528vector\u7b49\u7ed3\u6784\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u6570\u7ec4\u672c\u8eab\u9650\u5236\u662f\u5f88\u591a\u7684\uff0c\u5fc5\u987b\u63d0\u524d\u8bbe\u7f6e\u597d\u6570\u636e\u7c7b\u578b\u548c\u957f\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type arrayName &#91; arraySize ];\/\/\u57fa\u7840\u6570\u7ec4\u7ed3\u6784\ndouble balance&#91;10];\/\/\u5b9a\u4e49\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\ndouble balance&#91;5] = {1000.0, 2.0, 3.4, 7.0, 50.0};\/\/\u5728\u5b9a\u4e49\u65f6\u4e00\u5e76\u8d4b\u503c\n\n\/\/\u6570\u7ec4\u957f\u5ea6\u4e0d\u80fd\u4e3a\u53d8\u91cf\uff0c\u5fc5\u987b\u4f7f\u7528const\nconst int n = 3;\nint a&#91;n] = { 1,2,3 };\nstd::cout &lt;&lt; a&#91;0] &lt;&lt; std::endl;\n\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528new\u65b9\u6cd5\u5b9e\u73b0\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u662f\u6570\u7ec4\u5b9e\u73b0\u7ebf\u6027\u8868\u7684\u57fa\u7840<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int *psome = new int&#91;10];\npsome&#91;0]=0;\npsome&#91;1]=1;\ndelete &#91;] psome ;\/\/\u521b\u7acb\u52a8\u6001\u6570\u7ec4\u4e00\u5b9a\u8981\u8bb0\u5f97\u5220\u9664<\/code><\/pre>\n\n\n\n<p>\u6709\u4e86\u52a8\u6001\u6570\u7ec4\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u518d\u8fdb\u4e00\u6b65\u5199\u51fa\u66f4\u6539\u6570\u7ec4\u957f\u5ea6\u7684\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/shuzu.cpp\nusing namespace std;\ntemplate&lt;class T&gt;\nvoid changeLength1D(T*&amp; a, int oldLength, int newLength) {\n\tT* temp = new T&#91;newLength];\n\tint number = fmin(oldLength, newLength);\n\tcopy(a, a + number, temp);\n\tdelete&#91;]a;\n\ta = temp;\n}\n\/\/shuzu.h\ntemplate&lt;class T&gt;\nvoid changeLength1D(T*&amp;, int, int);\n\/\/main.cpp\n#include &lt;iostream&gt;\n#include \"shuzu.h\"\n#include \"shuzu.cpp\"\nusing namespace std;\nint main()\n{   \n\tint* a = new int&#91;5]{1,2,3,4,5};\/\/\u5fc5\u987b\u662f\u52a8\u6001\u6570\u7ec4\n\tchangeLength1D(a, 5, 3);\n}\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u6a21\u677f\u7c7b\u4ee5\u5e94\u5bf9\u4e0d\u540c\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5b9e\u73b0\u4e86\u4e00\u5b9a\u7684\u901a\u7528\u6027\u3002\u9664\u6b64\u4e4b\u5916\u51fd\u6570\u5728\u63a5\u53d7\u53c2\u6570\u65f6\u4f7f\u7528\u4e86\u5f15\u7528\u7684\u6280\u5de7\uff0c T*&amp; a \u7684\u542b\u4e49\u65f6\u83b7\u5f97\u6307\u9488\u7684\u5f15\u7528\uff0ca\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u5982\u679c\u4e0d\u901a\u8fc7\u5f15\u7528\uff0c\u6211\u4eec\u53ea\u80fd\u66f4\u6539a\u6307\u5411\u7684\u53d8\u91cf\u7684\u6570\u503c\uff0c\u4e0d\u80fd\u6539\u53d8a\u7684\u6307\u5411\uff0c\u8fd9\u6837\u5c31\u6ca1\u6cd5\u5728\u540e\u9762\u5220\u9664a\u6307\u5411\u7684\u6570\u7ec4\u518d\u4ee4a\u6307\u5411temp\u6307\u5411\u7684\u6570\u7ec4\u4e86\u3002\u901a\u8fc7\u6307\u9488\u7684\u6307\u9488\u4e5f\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class T>\nvoid changeLength1D(T** a, int oldLength, int newLength) {\n\tT* temp = new T&#91;newLength];\n\tint number = fmin(oldLength, newLength);\n\tcopy(*a, *a + number, temp);\n\tdelete&#91;]*a;\n\t*a = temp;\n}\nint main()\n{   \n\tint* a = new int&#91;5]{1,2,3,4,5};\n\tchangeLength1D(&amp;a, 5, 3);\n}\n<\/code><\/pre>\n\n\n\n<p>\u6709\u4e86\u53d8\u957f\u51fd\u6570\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9a\u4e49arraylist\u7c7b\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class T&gt;\nclass arrayList {\npublic:\n\tarrayList(int initialcapacity = 10);\n\tarrayList(const arrayList&lt;T&gt;&amp;);\n\t~arrayList(){delete &#91;]element;}\n\tbool empty() const { return listSize == 0; }\n\tint size() const { return listSize; }\n\tT&amp; get(int theIndex) const;\n\tvoid arrayList&lt;T&gt;::erase(int);\n\tvoid arrayList&lt;T&gt;::insert(int, const T&amp;);\nprotected:\n\tT* element;\n\tint arrayLength;\n\tint listSize;\n};<\/code><\/pre>\n\n\n\n<p>\u6784\u9020\u51fd\u6570\uff1a\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d\u5b9e\u73b0\u521b\u5efa\u548c\u590d\u5236\u529f\u80fd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class T&gt;\narrayList&lt;T&gt;::arrayList(int initialCapacity) {\n\tarrayLength = initialCapacity;\n\telement = new T&#91;arrayLength];\n\tlistSize = 0;\n}\n\ntemplate&lt;class T&gt;\narrayList&lt;T&gt;::arrayList(const arrayList&lt;T&gt;&amp; theList) {\/\/\u4f7f\u7528\u5f15\u7528\u6765\u907f\u514d\u590d\u5236\u63d0\u9ad8\u6548\u7387\uff0cconst\u907f\u514d\u4e0d\u5c0f\u5fc3\u4fee\u6539\n\tarrayLength = theList.arrayLength;\n\telement = new T&#91;arrayLength];\n\tlistSize = theList.listSize;\n\tcopy(theList.element, theList.element + listSize, element);\n}<\/code><\/pre>\n\n\n\n<p>\u67e5\u627e\u548c\u83b7\u53d6\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u90fd\u5f88\u7b80\u5355\uff0c\u5220\u9664\u548c\u63d2\u5165\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u5220\u9664\u9700\u8981\u628a\u5220\u9664\u540e\u7684\u5143\u7d20\u5411\u5de6\u79fb\uff0c\u63d2\u5165\u4e0d\u4ec5\u8981\u5e73\u79fb\uff0c\u8fd8\u5f97\u63d0\u524d\u5224\u65ad\u4e00\u4e0b\u957f\u5ea6\u591f\u4e0d\u591f\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class T&gt;\nvoid arrayList&lt;T&gt;::erase(int theIndex) {\n\tcopy(element + theIndex + 1, element + listSize, element + theIndex);\n\telement&#91;--listSize].~T();\n}\n\ntemplate&lt;class T&gt;\nvoid arrayList&lt;T&gt;::insert(int theIndex, const T&amp; theElement) {\n\tif (listSize == arrayLength) {\n\t\tchangeLength1D(element, arrayLength, 2 * arrayLength);\n\t\tarrayLength *= 2;\n\t}\n\tcopy_backward(element + theIndex, element + listSize, element + listSize + 1);\/\/\u5fc5\u987b\u4ece\u6700\u540e\u4e00\u4e2a\u5f00\u59cb\u5e73\u79fb\n\telement&#91;theIndex] = theElement;\n}<\/code><\/pre>\n\n\n\n<p>\u5176\u4ed6\u4e00\u4e9b\u51fd\u6570\u6bd4\u5982\u8f93\u51fa\u554a\uff0c\u68c0\u67e5\u554a\u5565\u7684\u5c31\u4e0d\u5199\u4e86~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u7ec4\u5b9e\u73b0\u7684\u7ebf\u6027\u8868<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-5"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":6,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":272,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/43\/revisions\/272"}],"wp:attachment":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}