{"rsdb":{"rid":"239921","subhead":"","postdate":"0","aid":"165310","fid":"49","uid":"1","topic":"1","content":"
\n

C\u3001C++<\/a>\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0<\/h1> \n

\u4e25\u683c\u6765\u8bf4\uff0c\u4e0a\u4e00\u4e2a\u7248\u672c\u4e0d\u80fd\u7b97\u662f\u7eaf\u7cb9\u7684C\u8bed\u8a00<\/a>\u7248\u672c\uff0c\u8fd9\u662f\u56e0\u4e3a\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86c++\u7684\u5f15\u7528\u7279\u6027\uff0c\u8fd9\u662fC\u8bed\u8a00<\/a>\u6240\u4e0d\u5305\u542b\u7684\u3002\u7136\u800c\uff0c\u8fd9\u662f\u7531\u4e8e\u6d4b\u8bd5\u4ee3\u7801\u7684\u9650\u5236\uff0c\u56e0\u800c\u6211\u4eec\u8fd8\u662f\u628a\u5b83\u770b\u505aC\u8bed\u8a00\u7684\u5b9e\u73b0\u3002\uff08\u4e5f\u53ef\u4ee5\u7f16\u5199\u4e00\u79cd\u4e0d\u5305\u542b\u5f15\u7528\u7684\u4ee3\u7801\u6765\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\uff0c\u8fd9\u8981\u6c42\u4f7f\u7528\u5230\u5b8f\u5b9a\u4e49\u548c\u4e00\u79cd\u79f0\u4e4b\u4e3a\u201cwrapper\u201d\u7684\u5c0f\u6280\u5de7\uff09<\/p> \n

\u95f2\u8bdd\u5c11\u53d9\uff0c\u5148\u653e\u51fa\u65b0\u7684\u6d4b\u8bd5\u4ee3\u7801\uff0c\u518d\u5177\u4f53\u8ba8\u8bba\u5404\u4e2a\u51fd\u6570\u7684\u6539\u5199\u65b9\u6cd5\u3002<\/p> \n

\r\n\/\/LibArray.cpp\r\n\/\/ \u5b9e\u9a8c\u5185\u5bb9\uff1a\r\n\/\/ 1\uff1a\u5c06C\u8bed\u8a00\u7248\u672cLibArray\u7528C++<\/a>\u5c01\u88c5\uff0c\u6ce8\u610f\uff0c\u539fC\u7248\u672c\u4fdd\u7559\u4e00\u4e2a\u5907\u4efd\r\n\r\n\/\/ \u5b9e\u9a8c\u76ee\u7684\uff1a\r\n\/\/ 1\uff1aC++\u7c7b\u5b9a\u4e49\u7684\u57fa\u672c\u65b9\u6cd5\r\n\r\n\/\/ \u53ea\u63d0\u4ea4CLibArray.cpp\u53caCLibArray.h\r\n\r\n#include "stdafx.h"\r\n#include \n  \n   \r\n#include "CLibArray.h"\r\n\r\nint _tmain(int argc, _TCHAR* argv[])\r\n{\r\n    CArray array;\r\n    \/\/ \u4e0d\u518d\u9700\u8981initial\uff0c\u4f46\u5e94\u8be5\u6709\u6b63\u786e\u7684\u521d\u59cb\u5316\r\n    \/\/ array_initial(array); \r\n\r\n    \/\/array.recap(10); \r\n    \/\/assert(array.capacity() == 10); \r\n\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    for (int i = 0; i < 20; ++i)\r\n    {\r\n        array.append(i); \r\n    }\r\n    assert(array.size() == 20); \r\n\r\n    for (int i = 0; i < array.size(); ++i)\r\n    {\r\n        assert(array.at(i) == i); \r\n    }\r\n\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    CArray array2, array3; \r\n    \/\/ array_initial(array2); \r\n    \/\/ array_initial(array3); \r\n\r\n    array2.copy(array); \r\n    assert(array.compare(array2) == true); \r\n\r\n    array3.copy(array); \r\n    assert(array.compare(array3) == true); \r\n\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    array2.insert(2, 3); \r\n    assert(array.compare(array2) == false); \r\n\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    array3.at(2) = 5; \r\n    assert(array.compare(array3) == false); \r\n\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    \/\/ \u4e0d\u518d\u9700\u8981destroy\uff0c\u4f46\u5e94\u8be5\u6709\u6b63\u786e\u7684\u5185\u5b58\u91ca\u653e\r\n    \/\/ array_destroy(array); \r\n    \/\/ array_destroy(array2); \r\n    \/\/ array_destroy(array3); \r\n\r\n    return 0;\r\n}\n  <\/assert.h><\/pre> \n 

\u7c7b\u7684\u5b9a\u4e49<\/h2> \n

\u4ece\u6d4b\u8bd5\u4ee3\u7801\u548c\u6ce8\u91ca\u4e2d\u53ef\u89c1\uff0c\u5bf9\u53d8\u91cf array\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5408\u9002\u7684 CArray \u7c7b\u5373\u53ef\u3002\u800c\u5728\u7c7b\u7684\u5b9a\u4e49\u90e8\u5206\uff0c\u5c06\u539f\u6709\u7684\u7ed3\u6784\u4f53\u6210\u5458\u653e\u5230 private \u79c1\u6709\u6570\u636e\u6210\u5458\uff1a<\/p> \n

\r\ntypedef int TypeName;\r\nconst int INITLENGTH = 10; \/\/\u521d\u59cb\u5316\u957f\u5ea6\u53ef\u4ee5\u4e3a\u4efb\u610f\u6b63\u6574\u6570,\u4e5f\u53ef\u4ee5\u4e3a\u96f6\uff0c\u4f46\u9700\u8981\u628aappend\u51fd\u6570\u91cc\u7684\u8bed\u53e5\u505a\u9002\u5f53\u4fee\u6539\r\n\r\nprivate:\r\n    TypeName *arrayhead;\r\n    int arraysize;\r\n    int arraycapacity;<\/pre> \n 

\u81ea\u7136\u800c\u7136\u5730\uff0c\u6211\u4eec\u9700\u8981\u5c06C\u8bed\u8a00\u7248\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u653e\u5230 CArray \u7c7b\u7684\u516c\u6709\u90e8\u5206\u4f5c\u4e3a\u63a5\u53e3\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u6ce8\u610f\u5230\u6ce8\u91ca\u90e8\u5206\uff1a\u4e0d\u518d\u9700\u8981 initial \u548c destroy \u51fd\u6570\uff0c\u5b66\u8fc7c++\u7684\u76c6\u53cb\u4eec\u90fd\u77e5\u9053\uff0c\u8fd9\u662f\u5f88\u81ea\u7136\u7684\uff0c\u56e0\u4e3a\u5728\u4f7f\u7528c++
\u7f16\u7a0b<\/a>\u65f6\uff0c\u4e00\u822c\u9700\u8981\u7ed9\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u5199\u597d\u5bf9\u5e94\u7684\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\uff0c\u5b9e\u9645\u4e0a\u8fd9\u6837\u7684\u4e24\u79cd\u51fd\u6570\uff0c\u5c31\u5bf9\u5e94\u4e8e\u539f\u6765\u7684 initial \u548c destroy \u51fd\u6570\u3002
\u53e6\u5916\uff0c\u6ce8\u610f\u5230\u6d4b\u8bd5\u6587\u4ef6\u4e0d\u518d\u5305\u542b\u4e0b\u5217\u8bed\u53e5 array.recap(10);\u7531\u4e8e\u5728C\u8bed\u8a00\u5b9e\u73b0\u4e2d\uff0c recap \u51fd\u6570\u7528\u4e8e\u7ed9\u52a8\u6001\u6570\u7ec4\u8d4b\u4e88\u4e00\u5b9a\u7684\u7a7a\u95f4\u5927\u5c0f\uff0c \u800c\u6d4b\u8bd5\u51fd\u6570\u4e2d\u53d6\u6d88\u4e86\u6b64\u8bed\u53e5\uff0c\u90a3\u4e48\u5c31\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u64cd\u4f5c\uff0c\u4e00\u79cd\u662f\u8981\u8003\u8651\u5c06\u7a7a\u95f4\u7684\u5206\u914d\u653e\u5728\u5176\u4ed6\u7684\u51fd\u6570\u4e2d\uff0c\u6216\u8005\u4fdd\u7559 recap \u51fd\u6570\uff0c\u518d\u8ba9\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u5b83\u3002\u8fd9\u6837\u7684\u7279\u6027\u5f88\u7b26\u5408\u7c7b\u7684 protected \u65b9\u6cd5\u7684\u5b9a\u4e49\u3002\uff08\u5f53\u7136\uff0c\u82e5\u4e0d\u8003\u8651\u7ee7\u627f\uff0c\u5c06\u5176\u4f5c\u4e3a private \u65b9\u6cd5\u4e5f\u672a\u5c1d\u4e0d\u53ef\uff09
\u4e8e\u662f\u603b\u4f53\u601d\u8def\u6e05\u6670\u4e86\uff1a\u5728 private \u6210\u5458\u4e2d\u5b9a\u4e49\u4e86\u52a8\u6001\u6570\u7ec4\u7684\u5fc5\u8981\u53c2\u6570\uff0c\u5728 public \u90e8\u5206\u5b9a\u4e49\u4e86\u53ef\u4ee5\u8fdb\u884c\u7684\u64cd\u4f5c\uff1a<\/p> \n

\r\npublic:\r\n    CArray();\r\n    ~CArray();\r\n    inline int capacity() { return arraycapacity; };\r\n    inline int size() { return arraysize; };\r\n    inline TypeName& at(int num) { return arrayhead[num]; };\r\n    void append(int num);\r\n    void copy(CArray &another);\r\n    bool compare(CArray &another);\r\n    void insert(int num, TypeName value);\r\n\r\nprotected:\r\n    void recap(int length);\r\n    void printarray();<\/pre> \n 

\u7b80\u77ed\u7684\u51fd\u6570\u76f4\u63a5\u5b9a\u4e49\u4e3a\u5185\u8054\u51fd\u6570\uff08\u6ce8\u610f\u82e5\u76f4\u63a5\u5199\u5728\u7c7b\u5b9a\u4e49\u7684\u5934\u6587\u4ef6\u5185\uff0c\u5219\u65e0\u9700 inline \u5173\u952e\u5b57\uff0c\u7136\u800c\u662f\u5426\u6700\u7ec8\u7f16\u8bd1\u4e3a\u5185\u8054\u51fd\u6570\uff0c\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u5173\u4e8e inline \u5173\u952e\u5b57\uff09<\/p> \n

\u51fd\u6570 printarray \u7528\u6765\u8f93\u51fa\u52a8\u6001\u6570\u7ec4\u7684\u5173\u952e\u4fe1\u606f\u3002<\/p> \n

\u5728\u6539\u5199\u51fd\u6570\u65f6\uff0c\u4e3b\u8981\u5de5\u4f5c\u662f\u4fee\u6539\u5176\u53c2\u6570\uff0c\u5e76\u5728\u5177\u4f53\u7684\u5b9a\u4e49\u4e2d\u7701\u53bb\u5bf9\u8c03\u7528\u5bf9\u8c61\u672c\u8eab\u7684\u663e\u5f0f\u8868\u793a\uff08\u4e5f\u53ef\u4ee5\u91c7\u7528 this \u6307\u9488\u6765\u5b8c\u6210\uff09\u3002<\/p> \n

\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570<\/h3> \n

\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u9009\u62e9\u7ed9\u5934\u6307\u9488\u5206\u914d\u4e00\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\uff0c\u4e5f\u53ef\u4ee5\u8d4b\u503c\u4e3a\u7a7a\uff08nullptr\uff0c\u5373C\u8bed\u8a00\u4e2d\u7684NULL\uff09\uff0c\u51fa\u4e8e\u4e00\u79cd\u5408\u60c5\u5408\u7406\u7684\u539f\u56e0\uff0c\u6211\u7ed9\u5b83\u5206\u914d\u4e86\u4e00\u5b9a\u7684\u5927\u5c0f\u3002
\u5728 c++\u4e2d\uff0c\u4f7f\u7528 new \u548c delete \u6765\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\uff0c<\/p> \n

\r\nCArray::CArray()\r\n{\r\n    arrayhead = new TypeName[INITLENGTH];\r\n    arraysize = 0;\r\n    arraycapacity = INITLENGTH;\r\n}<\/pre> \n 

\u800c\u5bf9\u4e8e\u6790\u6784\u51fd\u6570\uff0c\u53ea\u8981\u76f8\u5e94\u5730\u91ca\u653e\u5185\u5b58\u5373\u53ef\uff1a<\/p> \n

\r\nCArray::~CArray()\r\n{\r\n    delete[] arrayhead;\r\n    arrayhead = nullptr;\r\n    arraycapacity = 0;\r","orderid":"0","title":"C\u3001C++\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0(\u4e00)","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"563","pages":"3","comments":"0","posttime":"2017-10-21 06:06:50","list":"1508537210","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u52a8\u6001<\/A> \u5b9e\u73b0<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.108.110.181","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"C\u3001C++\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0","lastview":"1713965922","digg_num":"3","digg_time":"1710466789","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}