{"id":250,"date":"2012-02-23T10:32:14","date_gmt":"2012-02-23T10:32:14","guid":{"rendered":"http:\/\/ahsiu.com\/?p=250"},"modified":"2012-02-23T10:32:14","modified_gmt":"2012-02-23T10:32:14","slug":"c-vector%e5%ae%b9%e5%99%a8%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=250","title":{"rendered":"C++ vector\u5bb9\u5668\u7c7b\u578b"},"content":{"rendered":"<div id=\"cnblogs_post_body\">\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">vector\u7c7b\u4e3a\u5185\u7f6e\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u66ff\u4ee3\u8868\u793a\uff0c\u4e0estring\u7c7b\u4e00\u6837 vector \u7c7b\u662f\u968f\u6807\u51c6 C++\u5f15\u5165\u7684\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206 \uff0c\u4e3a\u4e86\u4f7f\u7528vector \u6211\u4eec\u5fc5\u987b\u5305\u542b\u76f8\u5173\u7684\u5934\u6587\u4ef6\u00a0 \uff1a<\/span><\/p>\n<p><span style=\"color: #ff0000; font-family: \u5b8b\u4f53; font-size: small;\">#include &lt;vector&gt;<\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">\u4f7f\u7528vector\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u5f62\u5f0f\uff0c\u5373\u6240\u8c13\u7684<span style=\"color: #0000ff;\">\u6570\u7ec4\u4e60\u60ef<\/span>\u548c <span style=\"color: #0000ff;\">STL\u4e60\u60ef<\/span>\u3002<\/span><\/p>\n<h1><span style=\"color: #800040;\">\u4e00\u3001\u6570\u7ec4\u4e60\u60ef\u7528\u6cd5<\/span><\/h1>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">1. \u5b9a\u4e49\u4e00\u4e2a\u5df2\u77e5\u957f\u5ea6\u7684 vector \uff1a<\/span><\/p>\n<p><span style=\"color: #ff0000; font-family: \u5b8b\u4f53; font-size: small;\">vector&lt; int &gt; ivec<span style=\"color: #0000ff;\">( 10 );<\/span>\u00a0 \/\/\u7c7b\u4f3c\u6570\u7ec4\u5b9a\u4e49int ia<span style=\"color: #0000ff;\">[ 10 ]; <\/span><\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">\u53ef\u4ee5\u901a\u8fc7ivec[<span style=\"color: #0000ff;\">\u7d22\u5f15\u53f7<\/span>] \u6765\u8bbf\u95ee\u5143\u7d20<\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">\u4f7f\u7528 <span style=\"color: #ff0000;\">if ( ivec.empty() )<\/span> \u5224\u65ad\u662f\u5426\u662f\u7a7a\uff0c<span style=\"color: #ff0000;\">ivec.size()<\/span>\u5224\u65ad\u5143\u7d20\u4e2a\u6570\u3002<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">2. vector\u7684\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a\u4e0e\u5176\u7c7b\u578b\u76f8\u5173\u7684\u7f3a\u7701\u503c\uff1a<span style=\"color: #0000ff;\">\u7b97\u672f\u548c\u6307\u9488\u7c7b\u578b\u7684\u7f3a\u7701\u503c\u662f 0\uff0c\u5bf9\u4e8eclass \u7c7b\u578b\uff0c\u7f3a\u7701\u503c\u53ef\u901a\u8fc7\u8c03\u7528\u8fd9\u7c7b\u7684\u7f3a\u7701\u6784\u9020\u51fd\u6570\u83b7\u5f97<\/span>\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5143\u7d20\u63d0\u4f9b\u4e00\u4e2a\u663e\u5f0f\u7684\u521d\u59cb\u503c\u6765\u5b8c\u6210\u521d\u59cb\u5316\uff0c\u4f8b\u5982<br \/>\n<span style=\"color: #ff0000;\">vector&lt; int &gt; ivec( 10, -1 );<br \/>\n<\/span>\u5b9a\u4e49\u4e86 ivec \u5b83\u5305\u542b\u5341\u4e2aint\u578b\u7684\u5143\u7d20 \u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u521d\u59cb\u5316\u4e3a-1\u00a0 <\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">\u5bf9\u4e8e\u5185\u7f6e\u6570\u7ec4 \u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u628a\u6570\u7ec4\u7684\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u4e00\u7ec4\u5e38\u91cf\u503c\uff0c\u4f8b\u5982 \uff1a<br \/>\n<span style=\"color: #0000ff;\">int ia[ 6 ] = { -2, -1, 0, 1, 2, 1024 };<\/span><\/span><br \/>\n<span style=\"color: #ff0000;\">\u6211\u4eec\u4e0d\u80fd\u7528\u540c\u6837\u7684\u65b9\u6cd5\u663e\u5f0f\u5730\u521d\u59cb\u5316 vector<\/span> \uff0c\u4f46\u662f\u53ef\u4ee5\u5c06 vector \u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5df2\u6709\u6570\u7ec4\u7684\u5168\u90e8\u6216\u4e00\u90e8\u5206\uff0c\u53ea\u9700\u6307\u5b9a\u5e0c\u671b\u88ab\u7528\u6765\u521d\u59cb\u5316 vector \u7684\u6570\u7ec4\u7684\u5f00\u59cb\u5730\u5740\u4ee5\u53ca\u6570\u7ec4\u6700\u672b\u5143\u7684\u4e0b\u4e00\u4f4d\u7f6e\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<br \/>\n<span style=\"color: #ff0000;\">\/\/ \u628a ia \u7684 6 \u4e2a\u5143\u7d20\u62f7\u8d1d\u5230 ivec \u4e2d<br \/>\nvector&lt; int &gt; ivec( ia, ia+6 );<\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\"><br \/>\n\u88ab\u4f20\u9012\u7ed9ivec \u7684\u4e24\u4e2a\u6307\u9488\u6807\u8bb0\u4e86\u7528\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u503c\u7684\u8303\u56f4\uff0c<span style=\"color: #0000ff;\">\u7b2c\u4e8c\u4e2a\u6307\u9488\u603b\u662f\u6307\u5411\u8981\u62f7\u8d1d\u7684\u672b\u5143\u7d20\u7684\u4e0b\u4e00\u4f4d\u7f6e<\/span>\uff0c\u6807\u8bb0\u51fa\u6765\u7684\u5143\u7d20\u8303\u56f4\u4e5f\u53ef\u4ee5\u662f\u6570\u7ec4\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u4f8b\u5982 :<\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\"><span style=\"color: #ff0000;\">\/\/ \u62f7\u8d1d 3 \u4e2a\u5143\u7d20 ia[2], ia[3], ia[4]<br \/>\nvector&lt; int &gt; ivec( &amp;ia[ 2 ], &amp;ia[ 5 ] ); <\/span><\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\"><span style=\"color: #ff0000;\"><br \/>\n<\/span>3. \u4e0e\u5185\u7f6e\u6570\u7ec4\u4e0d\u540c vector \u53ef\u4ee5\u88ab\u53e6\u4e00\u4e2a vector \u521d\u59cb\u5316 \u6216\u88ab\u8d4b\u7ed9\u53e6\u4e00\u4e2a vector \u4f8b\u5982<br \/>\n<span style=\"color: #ff0000;\">vector&lt; string &gt; svec;<br \/>\nvoid init_and_assign()<br \/>\n{<br \/>\n\/\/ \u7528\u53e6\u4e00\u4e2a vector \u521d\u59cb\u5316\u4e00\u4e2a vector<br \/>\nvector&lt; string &gt; user_names( svec );<br \/>\n\/\/ &#8230;<\/p>\n<p>\/\/ \u628a\u4e00\u4e2a vector \u62f7\u8d1d\u7ed9\u53e6\u4e00\u4e2a vector<br \/>\nsvec = user_names;<br \/>\n} <\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<h1><span style=\"color: #800040;\">\u4e8c\u3001STL\u4e60\u60ef\u7528\u6cd5<\/span><\/h1>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">\u5728 STL9\u4e2d\u5bf9vector \u7684\u4e60\u60ef\u7528\u6cd5\u5b8c\u5168\u4e0d\u540c\u3002\u6211\u4eec\u4e0d\u662f\u5b9a\u4e49\u4e00\u4e2a\u5df2\u77e5\u5927\u5c0f\u7684 vector\uff0c\u800c\u662f\u5b9a\u4e49\u4e00\u4e2a\u7a7a vector<br \/>\n<span style=\"color: #ff0000;\">vector&lt; string &gt; text; <\/span><\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\"><span style=\"color: #ff0000;\"><br \/>\n<\/span>1. \u6211\u4eec\u5411 vector \u4e2d\u63d2\u5165\u5143\u7d20\uff0c\u800c\u4e0d\u518d\u662f\u7d22\u5f15\u5143\u7d20\uff0c\u4ee5\u53ca\u5411\u5143\u7d20\u8d4b\u503c\uff0c\u4f8b\u5982 push_back()\u64cd\u4f5c\uff0c\u5c31\u662f\u5728 vector \u7684\u540e\u9762\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u4e0b\u9762\u7684 while \u5faa\u73af\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\u5e76\u6bcf\u6b21\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u63d2\u5165\u5230 vector \u4e2d<br \/>\n<span style=\"color: #ff0000;\">string word;<br \/>\nwhile ( cin &gt;&gt; word ) {<br \/>\ntext.push_back( word );<br \/>\n\/\/ &#8230;<br \/>\n} <\/span><\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\"><span style=\"color: #0000ff;\">\u867d\u7136\u6211\u4eec\u4ecd\u53ef\u4ee5\u7528\u4e0b\u6807\u64cd\u4f5c\u7b26\u6765\u8fed\u4ee3\u8bbf\u95ee\u5143\u7d20<br \/>\n<\/span>cout &lt;&lt; &#8220;words read are: n&#8221;;<\/p>\n<p>for ( int ix = 0; ix &lt; text.size(); ++ix )<br \/>\ncout &lt;&lt; text[ ix ] &lt;&lt; &#8216; &#8216;;<\/p>\n<p>cout &lt;&lt; endl;<br \/>\n\u4f46\u662f \u66f4\u5178\u578b\u7684\u505a\u6cd5\u662f\u4f7f\u7528 vector \u64cd\u4f5c\u96c6\u4e2d\u7684<span style=\"color: #0000ff;\">begin()\u548c end()<\/span>\u6240\u8fd4\u56de\u7684\u8fed\u4ee3\u5668 <span style=\"color: #0000ff;\">iterator<\/span><br \/>\n\u5bf9 \uff1a<br \/>\ncout &lt;&lt; &#8220;words read are: n&#8221;;<\/p>\n<p>for ( vector&lt;string&gt;::iterator it = text.begin();<br \/>\nit != text.end(); ++it )<br \/>\ncout &lt;&lt; *it &lt;&lt; &#8216; &#8216;;<\/p>\n<p>cout &lt;&lt; endl<br \/>\niterator \u662f\u6807\u51c6\u5e93\u4e2d\u7684\u7c7b\uff0c\u5b83\u5177\u6709<span style=\"color: #0000ff;\">\u6307\u9488<\/span>\u7684\u529f\u80fd\u00a0 <\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\"><br \/>\n<span style=\"color: #ff0000;\">*it;<br \/>\n<\/span>\u5bf9\u8fed\u4ee3\u5668\u89e3\u5f15\u7528\uff0c\u5e76\u8bbf\u95ee\u5176\u6307\u5411\u7684\u5b9e\u9645\u5bf9\u8c61<br \/>\n<span style=\"color: #ff0000;\">++it; <\/span><\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">\u5411\u524d\u79fb\u52a8\u8fed\u4ee3\u5668 it \u4f7f\u5176\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">2. \u6ce8\u610f \u4e0d\u8981\u6df7\u7528\u8fd9\u4e24\u79cd\u4e60\u60ef\u7528\u6cd5\uff0c \u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u5b9a\u4e49<br \/>\n<span style=\"color: #ff0000;\">vector&lt; int &gt; ivec;<br \/>\n<\/span>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7a7avector \u518d\u5199\u8fd9\u6837\u7684\u8bed\u53e5<br \/>\n<span style=\"color: #ff0000;\">ivec[ 0 ] = 1024;<br \/>\n<\/span><span style=\"color: #0000ff;\">\u5c31\u662f\u9519\u8bef\u7684 \uff0c\u56e0\u4e3a ivec \u8fd8\u6ca1\u6709\u7b2c\u4e00\u4e2a\u5143\u7d20<\/span>\uff0c\u6211\u4eec\u53ea\u80fd\u7d22\u5f15 vector \u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u5143\u7d20 size()\u64cd\u4f5c\u8fd4\u56de vector \u5305\u542b\u7684\u5143\u7d20\u7684\u4e2a\u6570 \u3002<br \/>\n<\/span><\/p>\n<p><span style=\"font-family: \u5b8b\u4f53; font-size: small;\">3. \u7c7b\u4f3c\u5730 \u5f53\u6211\u4eec\u7528\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5927\u5c0f\u5b9a\u4e49\u4e00\u4e2a vector \u65f6\uff0c\u4f8b\u5982\u00a0 \uff1a<br \/>\n<span style=\"color: #ff0000;\">vector&lt;int&gt; ia( 10 );<\/span><br \/>\n\u4efb\u4f55\u4e00\u4e2a\u63d2\u5165\u64cd\u4f5c\u90fd\u5c06\u589e\u52a0vector \u7684\u5927\u5c0f\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u6389\u67d0\u4e2a\u73b0\u6709\u7684\u5143\u7d20\uff0c\u8fd9\u770b\u8d77\u6765\u597d\u50cf\u662f\u5f88\u663e\u7136\u7684\uff0c\u4f46\u662f \u4e0b\u9762\u7684\u9519\u8bef\u5728\u521d\u5b66\u8005\u4e2d\u5e76\u4e0d\u5c11\u89c1 \uff1a<br \/>\n<span style=\"color: #ff0000;\">const int size = 7;<br \/>\nint ia[ size ] = { 0, 1, 1, 2, 3, 5, 8 };<br \/>\nvector&lt; int &gt; ivec( size );<\/p>\n<p>for ( int ix = 0; ix &lt; size; ++ix )<br \/>\nivec.push_back( ia[ ix ]);<br \/>\n<\/span><span style=\"color: #0000ff;\">\u7a0b\u5e8f\u7ed3\u675f\u65f6ivec \u5305\u542b 14 \u4e2a\u5143\u7d20, ia \u7684\u5143\u7d20\u4ece\u7b2c\u516b\u4e2a\u5143\u7d20\u5f00\u59cb\u63d2\u5165\u3002<\/span><\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>vector\u7c7b\u4e3a\u5185\u7f6e\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u66ff\u4ee3\u8868\u793a\uff0c\u4e0estring\u7c7b\u4e00\u6837 vector \u7c7b\u662f\u968f\u6807\u51c6 C++\u5f15\u5165\u7684\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206 \uff0c\u4e3a\u4e86\u4f7f\u7528vector \u6211\u4eec\u5fc5\u987b\u5305\u542b\u76f8\u5173\u7684\u5934\u6587\u4ef6\u00a0 \uff1a #include &lt;vector&gt; \u4f7f\u7528vector\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u5f62\u5f0f\uff0c\u5373\u6240\u8c13\u7684\u6570\u7ec4\u4e60\u60ef\u548c STL\u4e60\u60ef\u3002 \u4e00\u3001\u6570\u7ec4\u4e60\u60ef\u7528\u6cd5 1. \u5b9a\u4e49\u4e00\u4e2a\u5df2\u77e5\u957f\u5ea6\u7684 vector \uff1a vector&lt; int &gt; ivec( 10 );\u00a0 \/\/\u7c7b\u4f3c\u6570\u7ec4\u5b9a\u4e49int ia[ 10 ]; \u53ef\u4ee5\u901a\u8fc7ivec[\u7d22\u5f15\u53f7] \u6765\u8bbf\u95ee\u5143\u7d20 \u4f7f\u7528 if ( ivec.empty() ) \u5224\u65ad\u662f\u5426\u662f\u7a7a\uff0civec.size()\u5224\u65ad\u5143\u7d20\u4e2a\u6570\u3002 &nbsp; 2. vector\u7684\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a\u4e0e\u5176\u7c7b\u578b\u76f8\u5173\u7684\u7f3a\u7701\u503c\uff1a\u7b97\u672f\u548c\u6307\u9488\u7c7b\u578b\u7684\u7f3a\u7701\u503c\u662f 0\uff0c\u5bf9\u4e8eclass \u7c7b\u578b\uff0c\u7f3a\u7701\u503c\u53ef\u901a\u8fc7\u8c03\u7528\u8fd9\u7c7b\u7684\u7f3a\u7701\u6784\u9020\u51fd\u6570\u83b7\u5f97\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5143\u7d20\u63d0\u4f9b\u4e00\u4e2a\u663e\u5f0f\u7684\u521d\u59cb\u503c\u6765\u5b8c\u6210\u521d\u59cb\u5316\uff0c\u4f8b\u5982 vector&lt; int &gt; ivec( 10, -1 ); \u5b9a\u4e49\u4e86 ivec \u5b83\u5305\u542b\u5341\u4e2aint\u578b\u7684\u5143\u7d20 \u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u521d\u59cb\u5316\u4e3a-1\u00a0 \u5bf9\u4e8e\u5185\u7f6e\u6570\u7ec4 \u6211\u4eec\u53ef\u4ee5\u663e\u5f0f\u5730\u628a\u6570\u7ec4\u7684\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u4e00\u7ec4\u5e38\u91cf\u503c\uff0c\u4f8b\u5982 \uff1a int ia[ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.xiunian.wang\/?p=250\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C++ vector\u5bb9\u5668\u7c7b\u578b&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/250"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":0,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}