{"id":303686,"date":"2024-05-20T19:05:35","date_gmt":"2024-05-20T11:05:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/303686.html"},"modified":"2024-05-20T19:05:45","modified_gmt":"2024-05-20T11:05:45","slug":"%e5%a6%82%e4%bd%95%e5%b0%86vectoriterator%e8%bd%ac%e6%8d%a2%e6%88%90int","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/303686.html","title":{"rendered":"\u5982\u4f55\u5c06vector::iterator\u8f6c\u6362\u6210int*"},"content":{"rendered":"<p style=\"text-align:center\">&lt;img src=&quot;https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26215929\/bff38a43-b023-4782-8196-9a506c578538.webp&quot; alt=&quot;\u5982\u4f55\u5c06vector::iterator\u8f6c\u6362\u6210int*&#8221; \/&gt;<\/p>\n<p><p>\u8981\u5c06<code>vector&lt;int&gt;::iterator<\/code>\u8f6c\u6362\u6210<code>int*<\/code>\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>&amp;(*iterator)<\/code>\u6765\u83b7\u53d6\u6307\u5411\u8fed\u4ee3\u5668\u5f53\u524d\u6307\u5411\u5143\u7d20\u7684\u6307\u9488\u3002<strong>\u8fd9\u79cd\u8f6c\u6362\u53ef\u80fd\u6709\u7528\uff0c\u5728\u9700\u8981\u4f20\u7edf\u6307\u9488\u63a5\u53e3\u7684\u9057\u7559\u4ee3\u7801\u4e2d\u5c24\u5176\u5e38\u89c1<\/strong>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a<code>std::vector&lt;int&gt; myVector<\/code>\u548c\u4e00\u4e2a\u5bf9\u5e94\u7684\u8fed\u4ee3\u5668<code>std::vector&lt;int&gt;::iterator it<\/code>\uff0c\u90a3\u4e48\u60a8\u53ef\u4ee5\u901a\u8fc7<code>int* ptr = &amp;(*it)<\/code>\u6765\u83b7\u53d6\u4e00\u4e2a\u6307\u5411<code>it<\/code>\u5f53\u524d\u6307\u5411\u7684\u5143\u7d20\u7684\u539f\u751f\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u7406\u89e3VECTOR\u548cITERATOR<\/strong><\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662fSTL\u5bb9\u5668\uff08\u5982<code>std::vector<\/code>\uff09\u63d0\u4f9b\u7684\u62bd\u8c61\uff0c\u5141\u8bb8\u6211\u4eec\u4ee5\u7edf\u4e00\u7684\u65b9\u5f0f\u5bf9\u5bb9\u5668\u7684\u5143\u7d20\u8fdb\u884c\u904d\u5386\u548c\u64cd\u4f5c\u3002\u8fed\u4ee3\u5668\u53ef\u4ee5\u89c6\u4e3a\u6307\u5411\u5bb9\u5668\u4e2d\u5143\u7d20\u7684\u6307\u9488\u3002\u4f46\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u91cd\u65b0\u5b9a\u4e49\u64cd\u4f5c\u7b26\uff0c\u4ee5\u4fbf\u4f7f\u7528++\u548c&#8211;\u904d\u5386\u5bb9\u5668\u3002\u5bf9\u4e8e<code>std::vector&lt;int&gt;<\/code>\uff0c\u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u6307\u5411\u6574\u6570\u7684\u7c7b\u6307\u9488\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u4eceITERATOR\u5230INT POINTER\u7684\u8f6c\u6362<\/strong><\/h3>\n<\/p>\n<p><p>\u8981\u5c06\u4e00\u4e2a<code>vector&lt;int&gt;::iterator<\/code>\u8f6c\u6362\u4e3a<code>int*<\/code>\u7c7b\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u53d6\u8fed\u4ee3\u5668\u6240\u6307\u5143\u7d20\u7684\u5730\u5740\u6765\u8fdb\u884c\u3002\u8fd9\u4e2a\u64cd\u4f5c\u6709\u6548\u662f\u56e0\u4e3a\u8fed\u4ee3\u5668\u6a21\u62df\u4e86\u6307\u9488\u7684\u884c\u4e3a\u3002\u53d6\u8fed\u4ee3\u5668\u6240\u6307\u5411\u5143\u7d20\u7684\u5730\u5740\u76f8\u5f53\u4e8e\u83b7\u53d6\u4e86\u4e00\u4e2a\u6307\u5411\u8be5\u5143\u7d20\u7684\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h4><strong>\u8f6c\u6362\u8fc7\u7a0b<\/strong><\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>&amp;(*iterator)<\/code>\u7684\u5f62\u5f0f\u76f4\u63a5\u83b7\u5f97\u60f3\u8981\u7684\u6307\u9488\u3002\u8fd9\u91cc<code>*iterator<\/code>\u8c03\u7528\u8fed\u4ee3\u5668\u7684\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u83b7\u53d6\u5230\u4e86\u5b83\u6240\u6307\u5411\u7684\u503c\u7684\u5f15\u7528\uff0c\u7136\u540e\u53d6\u8be5\u503c\u7684\u5730\u5740\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u6307\u9488\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">std::vector&lt;int&gt; v = {1, 2, 3, 4, 5};<\/p>\n<p>std::vector&lt;int&gt;::iterator it = v.begin();<\/p>\n<p>int* ptr = &amp;(*it); \/\/ ptr\u73b0\u5728\u6307\u5411v[0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u8fd9\u79cd\u8f6c\u6362\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u8fed\u4ee3\u5668\u4e0d\u662f\u6307\u5411vector\u7684<code>end()<\/code>\u4f4d\u7f6e\u4e4b\u540e\u7684\u3002\u56e0\u4e3a<code>end()<\/code>\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u6307\u5411\u7684\u662f\u5bb9\u5668\u672b\u5c3e\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u5bf9\u5176\u89e3\u5f15\u7528\u5c06\u662f\u672a\u5b9a\u4e49\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u8fed\u4ee3\u5668\u8f6c\u6362\u7684\u4f7f\u7528\u573a\u666f<\/strong><\/h3>\n<\/p>\n<p><p>\u867d\u7136\u73b0\u4ee3C++\u7a0b\u5e8f\u5efa\u8bae\u4f7f\u7528\u8fed\u4ee3\u5668\u6216\u5f15\u7528\u6765\u64cd\u4f5cSTL\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u5728\u65e7\u4ee3\u7801\u5e93\u6216\u9700\u8981\u4e0eC\u8bed\u8a00\u63a5\u53e3\u4ea4\u4e92\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u8fed\u4ee3\u5668\u8f6c\u6362\u4e3a\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h4><strong>\u4e0eC\u8bed\u8a00\u63a5\u53e3\u4e92\u64cd\u4f5c<\/strong><\/h4>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u8c03\u7528\u4e00\u4e2a\u671f\u5f85\u539f\u59cb\u6307\u9488\u53c2\u6570\u7684C\u8bed\u8a00\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ece\u8fed\u4ee3\u5668\u5230\u6307\u9488\u7684\u8f6c\u6362\u6765\u6ee1\u8db3\u8fd9\u4e00\u9700\u8981\uff0c\u8fd9\u6837\u907f\u514d\u4e86\u590d\u5236\u6574\u4e2avector\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4><strong>\u6027\u80fd\u5173\u952e\u4ee3\u7801<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u5bf9\u6027\u80fd\u8981\u6c42\u975e\u5e38\u9ad8\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u9700\u8981\u76f4\u63a5\u64cd\u4f5c\u6307\u9488\u4ee5\u51cf\u5c11\u8fed\u4ee3\u5668\u4ea7\u751f\u7684\u62bd\u8c61\u5f00\u9500\u7684\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5728\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f18\u5316\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u60c5\u51b5\u8d8a\u6765\u8d8a\u5c11\u89c1\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u6ce8\u610f\u4e0e\u9650\u5236<\/strong><\/h3>\n<\/p>\n<p><p>\u5f53\u6267\u884c\u8fed\u4ee3\u5668\u5230\u6307\u9488\u7684\u8f6c\u6362\u65f6\uff0c\u6709\u4e00\u4e9b\u6ce8\u610f\u8981\u70b9\u548c\u9650\u5236\u9700\u8981\u8003\u8651\u3002<\/p>\n<\/p>\n<p><h4><strong>\u8fed\u4ee3\u5668\u5931\u6548\u95ee\u9898<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u5bf9<code>vector<\/code>\u8fdb\u884c\u63d2\u5165\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u65f6\uff0c\u73b0\u6709\u7684\u8fed\u4ee3\u5668\u53ef\u80fd\u5931\u6548\u3002\u5931\u6548\u7684\u8fed\u4ee3\u5668\u5bf9\u5e94\u7684\u6307\u9488\u4e5f\u540c\u6837\u65e0\u6548\uff0c\u56e0\u6b64\u5728\u8f6c\u6362\u540e\u8981\u7279\u522b\u6ce8\u610f\u4e0d\u8981\u4f7f\u7528\u5931\u6548\u7684\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5bb9\u5668\u7684\u52a8\u6001\u53d8\u5316<\/strong><\/h4>\n<\/p>\n<p><p>\u7531\u4e8e<code>vector<\/code>\u53ef\u80fd\u4f1a\u5728\u6dfb\u52a0\u66f4\u591a\u5143\u7d20\u65f6\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488\u4e5f\u53ef\u80fd\u56e0\u4e3a\u91cd\u65b0\u5206\u914d\u800c\u5931\u6548\u3002\u6bcf\u5f53\u5bb9\u5668\u6709\u53ef\u80fd\u8fdb\u884c\u91cd\u65b0\u5206\u914d\u64cd\u4f5c\u65f6\uff0c\u90fd\u5e94\u91cd\u65b0\u8ba1\u7b97\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e94\u3001\u7ed3\u8bba<\/strong><\/h3>\n<\/p>\n<p><p>\u5c06<code>vector&lt;int&gt;::iterator<\/code>\u8f6c\u6362\u6210<code>int*<\/code>\u662f\u4e00\u79cd\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u5728C++\u4ee3\u7801\u4e2d\u5b9e\u73b0\u8fed\u4ee3\u5668\u4e0e\u539f\u59cb\u6307\u9488\u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u3002\u5c3d\u7ba1\u5b83\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u907f\u514d\u7531\u4e8e\u5bb9\u5668\u53d8\u52a8\u5f15\u8d77\u7684\u6307\u9488\u5931\u6548\u95ee\u9898\u3002\u4e13\u4e1a\u7684C++\u5f00\u53d1\u8005\u901a\u5e38\u503e\u5411\u4e8e\u4f7f\u7528\u8fed\u4ee3\u5668\u548c\u5f15\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u66f4\u52a0\u5b89\u5168\u3001\u7075\u6d3b\uff0c\u4e14\u4e0e\u5bb9\u5668\u7684\u884c\u4e3a\u5bc6\u5207\u76f8\u5173\u3002\u5f53\u5b9e\u9645\u9700\u8981\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u5e94\u786e\u4fdd\u8fed\u4ee3\u5668\u662f\u6709\u6548\u7684\uff0c\u5e76\u4e14\u5728\u5bb9\u5668\u751f\u547d\u5468\u671f\u5185\u4fdd\u6301\u8b66\u89c9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1a\u5982\u4f55\u5c06vector::iterator\u8f6c\u6362\u6210int\u6570\u7ec4\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u5c06vector::iterator\u8f6c\u6362\u4e3aint\u6570\u7ec4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2aint\u6307\u9488\uff0c\u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3avector\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u5730\u5740\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528vector\u7684begin()\u51fd\u6570\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u901a\u8fc7\u4f7f\u7528\u5730\u5740\u8fd0\u7b97\u7b26\uff08*\uff09\u83b7\u53d6\u8be5\u5143\u7d20\u7684\u5730\u5740\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386vector\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5143\u7d20\u7684\u503c\u590d\u5236\u5230int\u6570\u7ec4\u4e2d\u3002\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u5c06int\u6307\u9488\u9012\u589e\u4ee5\u6307\u5411\u4e0b\u4e00\u4e2aint\u5143\u7d20\u7684\u5730\u5740\uff0c\u5e76\u4f7f\u7528\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\uff08*\uff09\u5c06\u5143\u7d20\u7684\u503c\u590d\u5236\u5230\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u6700\u540e\uff0c\u60a8\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6240\u6709vector\u5143\u7d20\u7684int\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p><strong>\u95ee\u98982\uff1a\u5982\u4f55\u4f7f\u7528vector::iterator\u8bbf\u95ee\u5143\u7d20\u7684\u503c\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u4f7f\u7528vector::iterator\u8bbf\u95ee\u5143\u7d20\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u4f7f\u7528vector\u7684begin()\u51fd\u6570\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff0c\u4f7f\u7528vector\u7684end()\u51fd\u6570\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u8fd9\u5c06\u6784\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\u533a\u95f4\uff0c\u6db5\u76d6\u6574\u4e2avector\u7684\u5143\u7d20\u5e8f\u5217\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u8fed\u4ee3\u5668\u533a\u95f4\uff0c\u5e76\u4f7f\u7528\u89e3\u5f15\u7528\u8fd0\u7b97\u7b26\uff08*\uff09\u8bbf\u95ee\u6bcf\u4e2a\u8fed\u4ee3\u5668\u6307\u5411\u7684\u5143\u7d20\u7684\u503c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95eevector\u7684\u6240\u6709\u5143\u7d20\uff0c\u5e76\u5bf9\u5b83\u4eec\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><strong>\u95ee\u98983\uff1a\u5982\u4f55\u5224\u65advector::iterator\u662f\u5426\u6307\u5411\u4e86vector\u7684\u672b\u5c3e\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u5224\u65advector::iterator\u662f\u5426\u6307\u5411\u4e86vector\u7684\u672b\u5c3e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u4f7f\u7528vector\u7684end()\u51fd\u6570\u83b7\u53d6\u6307\u5411vector\u672b\u5c3e\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u8fed\u4ee3\u5668\u3002<\/li>\n<li>\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08==\uff09\u5c06vector::iterator\u4e0e\u8be5\u672b\u5c3e\u8fed\u4ee3\u5668\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u5b83\u4eec\u76f8\u7b49\uff0c\u5219\u8868\u793avector::iterator\u6307\u5411\u4e86vector\u7684\u672b\u5c3e\uff0c\u5426\u5219\u8868\u793a\u5b83\u6307\u5411\u4e86vector\u7684\u4e2d\u95f4\u5143\u7d20\u6216\u5f00\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u64cd\u4f5c\u6765\u5224\u65ad\u8fed\u4ee3\u5668\u662f\u5426\u6307\u5411\u4e86vector\u7684\u672b\u5c3e\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u7ec8\u6b62\u5faa\u73af\u6216\u8fdb\u884c\u5176\u4ed6\u903b\u8f91\u5904\u7406\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"&lt;img src=&quot;https:\/\/cdn-kb.worktile.com\/kb\/wp-con [&hellip;]","protected":false},"author":3,"featured_media":303697,"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\/303686"}],"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=303686"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303686\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/303697"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=303686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=303686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=303686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}