{"id":136145,"date":"2024-04-28T12:27:44","date_gmt":"2024-04-28T04:27:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/136145.html"},"modified":"2024-04-28T12:27:46","modified_gmt":"2024-04-28T04:27:46","slug":"c%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/136145.html","title":{"rendered":"c++\u5982\u4f55\u5b9a\u4e49\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24132908\/05cc8463-4622-406a-87d4-f9bc42846ab3.webp\" alt=\"c++\u5982\u4f55\u5b9a\u4e49\u6570\u7ec4\" \/><\/p>\n<p><p>\u5728C++\u4e2d\uff0c<strong>\u5b9a\u4e49\u6570\u7ec4\u662f\u4e00\u4e2a\u57fa\u7840\u800c\u91cd\u8981\u7684\u6982\u5ff5\u3001\u5b83\u5141\u8bb8\u60a8\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u9879\u76ee<\/strong>\u3002\u6700\u5e38\u89c1\u7684\u6570\u7ec4\u662f\u9759\u6001\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u7ecf\u786e\u5b9a\u3002\u7136\u800c\uff0cC++\u4e5f\u652f\u6301\u52a8\u6001\u6570\u7ec4\uff0c\u5b83\u4eec\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6539\u53d8\uff0c\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u8ba8\u8bba\u9759\u6001\u6570\u7ec4\u7684\u5b9a\u4e49\u65b9\u6cd5\u3002<strong>\u8981\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u6570\u7ec4\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u6301\u6709\u7684\u6570\u636e\u7c7b\u578b\u3001\u6570\u7ec4\u7684\u540d\u79f0\uff0c\u4ee5\u53ca\u65b9\u62ec\u53f7\u4e2d\u7684\u6570\u7ec4\u5927\u5c0f<\/strong>\u3002\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u662f\u4e00\u4e2a\u5927\u4e8e\u96f6\u7684\u6574\u6570\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u8981\u5b58\u50a810\u4e2a\u6574\u6570\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\u6570\u7ec4\uff1a<code>int myArray[10];<\/code> \u8fd9\u884c\u4ee3\u7801\u58f0\u660e\u4e86\u4e00\u4e2a\u540d\u4e3a<code>myArray<\/code>\u7684\u6570\u7ec4\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a810\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9759\u6001\u6570\u7ec4\u5b9a\u4e49<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u9759\u6001\u6570\u7ec4\u7684\u57fa\u672c\u8bed\u6cd5\u662f<strong>\u5148\u58f0\u660e\u6570\u7ec4\u7684\u7c7b\u578b\uff0c\u7136\u540e\u662f\u6570\u7ec4\u7684\u540d\u79f0\uff0c\u6700\u540e\u5728\u65b9\u62ec\u53f7[]\u5185\u6307\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f<\/strong>\u3002\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u9ed8\u8ba4\u503c\u53d6\u51b3\u4e8e\u6570\u7ec4\u7684\u7c7b\u578b\u548c\u6570\u7ec4\u58f0\u660e\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521d\u59cb\u5316\u6570\u7ec4<\/strong>\uff1a\u5728\u5b9a\u4e49\u6570\u7ec4\u65f6\uff0c\u60a8\u53ef\u4ee5\u540c\u65f6\u521d\u59cb\u5316\u6570\u7ec4\u3002\u4f8b\u5982\uff0c<code>int myArray[5] = {1, 2, 3, 4, 5};<\/code>\u8fd9\u6837\u4e0d\u4ec5\u5b9a\u4e49\u4e86\u6570\u7ec4\uff0c\u540c\u65f6\u4e5f\u521d\u59cb\u5316\u4e86\u6570\u7ec4\u4e2d\u7684\u4e94\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9ed8\u8ba4\u521d\u59cb\u5316<\/strong>\uff1a\u5982\u679c\u5728\u5b9a\u4e49\u65f6\u90e8\u5206\u521d\u59cb\u5316\u6570\u7ec4\uff0c\u672a\u88ab\u521d\u59cb\u5316\u7684\u5143\u7d20\u5c06\u88ab\u8bbe\u7f6e\u4e3a\u96f6\u3002\u4f8b\u5982\uff0c<code>int myArray[5] = {1, 2};<\/code>\u8fd9\u610f\u5473\u7740<code>myArray<\/code>\u7684\u524d\u4e24\u4e2a\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a1\u548c2\uff0c\u5269\u4f59\u7684\u5143\u7d20\u5c06\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u52a8\u6001\u6570\u7ec4\u5b9a\u4e49<\/h3>\n<\/p>\n<p><p>\u52a8\u6001\u6570\u7ec4\u5728C++\u4e2d\u901a\u8fc7\u6307\u9488\u548c\u65b0\u7684<strong>new<\/strong>\u64cd\u4f5c\u7b26\u6765\u5b9a\u4e49\uff0c\u5141\u8bb8\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u786e\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u6570\u7ec4\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528new\u64cd\u4f5c\u7b26\u5b9a\u4e49\u52a8\u6001\u6570\u7ec4<\/strong>\uff1a\u4f8b\u5982\uff0c<code>int* myArray = new int[10];<\/code>\u8fd9\u884c\u4ee3\u7801\u5728\u5806\u4e0a\u5206\u914d\u4e86\u8db3\u591f\u7684\u5185\u5b58\u6765\u5b58\u50a810\u4e2a\u6574\u6570\uff0c\u5e76\u4e14\u8fd4\u56de\u4e86\u4e00\u4e2a\u6307\u5411\u7b2c\u4e00\u4e2a\u6574\u6570\u7684\u6307\u9488\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91ca\u653e\u52a8\u6001\u6570\u7ec4<\/strong>\uff1a\u4f7f\u7528\u5b8c\u52a8\u6001\u6570\u7ec4\u540e\uff0c\u5e94\u4f7f\u7528delete []\u64cd\u4f5c\u7b26\u6765\u91ca\u653e\u5185\u5b58\u3002\u4f8b\u5982\uff0c<code>delete [] myArray;<\/code>\u8fd9\u6837\u505a\u53ef\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u6570\u7ec4\u7684\u4f7f\u7528\u548c\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u5b9a\u4e49\u4e86\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u6570\u7ec4\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8fd9\u610f\u5473\u7740\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f1\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6570\u7ec4\u540d\u79f0\u540e\u8ddf\u7d22\u5f15\u4f4d\u7f6e\u7684\u65b9\u62ec\u53f7\u6765\u8bbf\u95ee\u7279\u5b9a\u7684\u6570\u7ec4\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>myArray[0] = 10;<\/code>\u5c06\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u8bbe\u7f6e\u4e3a10\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u6570\u7ec4<\/strong>\uff1a\u6570\u7ec4\u901a\u5e38\u901a\u8fc7\u5faa\u73af\u7ed3\u6784\u904d\u5386\u3002\u4f8b\u5982\uff0c\u4f7f\u7528for\u5faa\u73af\u8bfb\u53d6\u6216\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879\u548c\u5e38\u89c1\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6570\u7ec4\u65f6\uff0c\u6709\u51e0\u4e2a\u91cd\u8981\u7684\u70b9\u9700\u8981\u6ce8\u610f\uff0c\u4ee5\u907f\u514d\u5e38\u89c1\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8d8a\u754c\u8bbf\u95ee<\/strong>\uff1a\u5c1d\u8bd5\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u5b9a\u4e49\u8303\u56f4\u7684\u5143\u7d20\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u53ef\u9884\u6d4b\u7684\u884c\u4e3a\u548c\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u7ec4\u5927\u5c0f<\/strong>\uff1a\u9759\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u5df2\u77e5\uff0c\u4e14\u4e00\u65e6\u5b9a\u4e49\uff0c\u5927\u5c0f\u4e0d\u53ef\u66f4\u6539\u3002\u5bf9\u4e8e\u9700\u8981\u52a8\u6001\u8c03\u6574\u5927\u5c0f\u7684\u5e94\u7528\u573a\u666f\uff0c\u5e94\u8003\u8651\u4f7f\u7528\u6807\u51c6\u6a21\u677f\u5e93\u4e2d\u7684\u5bb9\u5668\uff0c\u5982<code>std::vector<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001C++\u6807\u51c6\u5e93\u4e2d\u7684\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728C++\u6807\u51c6\u5e93\u4e2d\uff0c<code>std::array<\/code>\u548c<code>std::vector<\/code>\u63d0\u4f9b\u4e86\u6bd4\u539f\u59cb\u6570\u7ec4\u66f4\u5b89\u5168\u548c\u66f4\u7075\u6d3b\u7684\u66ff\u4ee3\u65b9\u6848\u3002<code>std::array<\/code>\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e\u9759\u6001\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u4f46\u589e\u52a0\u4e86\u4e00\u4e9b\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u7684\u6210\u5458\u51fd\u6570\u3002<code>std::vector<\/code>\u5219\u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\uff0c\u80fd\u591f\u6839\u636e\u9700\u8981\u81ea\u52a8\u589e\u957f\u548c\u6536\u7f29\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528std::array<\/strong>\uff1a<code>std::array&lt;int,5&gt; myArray = {1, 2, 3, 4, 5};<\/code><\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528std::vector<\/strong>\uff1a<code>std::vector&lt;int&gt; myVector = {1, 2, 3, 4, 5};<\/code><\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u5229\u7528\u8fd9\u4e9b\u73b0\u4ee3C++\u7279\u6027\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u5b89\u5168\u3001\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. C++\u4e2d\u5982\u4f55\u5b9a\u4e49\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u5b9a\u4e49\u6570\u7ec4\u662f\u5728C++\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u4ee5\u4e0b\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5b9a\u4e49\u6570\u7ec4\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-cpp\">\u6570\u636e\u7c7b\u578b \u6570\u7ec4\u540d[\u6570\u7ec4\u5927\u5c0f];\n<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b5\u4e2a\u6574\u6570\u7684\u6570\u7ec4\uff1a<\/p>\n<pre><code class=\"language-cpp\">int myArray[5];\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amyArray\u7684\u6574\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u5b58\u50a85\u4e2a\u6574\u6570\u503c\u3002<\/p>\n<p><strong>2. C++\u4e2d\u7684\u6570\u7ec4\u5982\u4f55\u521d\u59cb\u5316\uff1f<\/strong><\/p>\n<p>\u5728C++\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u5bf9\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n<ul>\n<li>\u9010\u4e2a\u8d4b\u503c\u521d\u59cb\u5316\uff1a\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u4ee5\u521d\u59cb\u503c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">int myArray[5];\nfor(int i=0; i&lt;5; i++){\n    myArray[i] = i+1;\n}\n<\/code><\/pre>\n<ul>\n<li>\u4f7f\u7528\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u5217\u8868\uff1a\u53ef\u4ee5\u5728\u5b9a\u4e49\u6570\u7ec4\u65f6\u4f7f\u7528\u82b1\u62ec\u53f7\u6765\u4e3a\u6570\u7ec4\u5143\u7d20\u63d0\u4f9b\u521d\u59cb\u503c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">int myArray[5] = {1, 2, 3, 4, 5};\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u521d\u59cb\u503c1\u30012\u30013\u30014\u548c5\u7684\u6574\u578b\u6570\u7ec4\u3002<\/p>\n<p><strong>3. C++\u4e2d\u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1f<\/strong><\/p>\n<p>\u8981\u8bbf\u95eeC++\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7684\u7d22\u5f15\u3002\u6570\u7ec4\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a1\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee\u4e00\u4e2a\u540d\u4e3amyArray\u7684\u6574\u578b\u6570\u7ec4\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre><code class=\"language-cpp\">int myArray[5] = {1, 2, 3, 4, 5};\nint element = myArray[2];\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u5c06\u7b2c\u4e09\u4e2a\u5143\u7d20\uff08\u7d22\u5f15\u4e3a2\uff09\u8d4b\u503c\u7ed9\u53d8\u91cfelement\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C++\u4e2d\uff0c\u5b9a\u4e49\u6570\u7ec4\u662f\u4e00\u4e2a\u57fa\u7840\u800c\u91cd\u8981\u7684\u6982\u5ff5\u3001\u5b83\u5141\u8bb8\u60a8\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u591a\u4e2a\u9879\u76ee\u3002\u6700\u5e38\u89c1\u7684\u6570\u7ec4\u662f\u9759\u6001\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u5728\u7f16 [&hellip;]","protected":false},"author":3,"featured_media":136146,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/136145"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=136145"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/136145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/136146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=136145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=136145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=136145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}