<span role="presentation"><span class="cm-meta">#include <vecoter></span></span>
<span role="presentation">{ </span>
<span role="presentation"> <span class="cm-variable">初始化</span></span>
<span role="presentation"> <span class="cm-comment">//vector从0下标开始;</span></span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">v,</span> <span class="cm-variable">v1</span>;<span class="cm-comment">//空容器</span></span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">v</span>(<span class="cm-number">3</span>)<span class="cm-comment">//包含3个元素;</span></span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">v</span>(<span class="cm-number">3</span>,<span class="cm-number">9</span>)<span class="cm-comment">//包含3个元素,每个元素都是9;</span></span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">v</span>(<span class="cm-variable">v1</span>)<span class="cm-comment">//copy v1;</span></span>
<span role="presentation"> <span class="cm-variable-3">int</span> <span class="cm-variable">a</span>[<span class="cm-number">3</span>] <span class="cm-operator">=</span> {<span class="cm-number">0</span>,<span class="cm-number">1</span>,<span class="cm-number">2</span>};</span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">v</span>(<span class="cm-variable">a</span>,<span class="cm-variable">a</span><span class="cm-operator">+</span><span class="cm-number">3</span>)<span class="cm-comment">//copy a数组;</span></span>
<span role="presentation"> </span>
<span role="presentation"> <span class="cm-variable">状态</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">size</span>()<span class="cm-comment">//返回v个数,空是时候是0,空时不可atuo i = v.begin();</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">begin</span>()<span class="cm-comment">//首地址</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">end</span>()<span class="cm-comment">//末元素地址+1;for里终止条件 auto i != v.end(); i < end(); i <= v.end()-1;</span></span>
<span role="presentation"> <span class="cm-comment">//有点像数组最后一位的空位置</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">at</span>(<span class="cm-variable">i</span>) <span class="cm-operator">==</span> <span class="cm-variable">v</span>[<span class="cm-variable">i</span>]<span class="cm-comment">//推荐使用后者;</span></span>
<span role="presentation"> <span class="cm-variable-3">bool</span> <span class="cm-variable">is</span> <span class="cm-operator">=</span> <span class="cm-variable">v</span>.<span class="cm-variable">empty</span>();<span class="cm-operator">/</span><span class="cm-variable">空返回true,不空返回false</span>;</span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">n</span>)<span class="cm-comment">//在尾部添加n进容器;</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">pop_back</span>()<span class="cm-comment">//删除尾元素;</span></span>
<span role="presentation"> <span class="cm-variable">二维容器</span></span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">>></span> <span class="cm-variable">v1</span>;</span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">v</span>[<span class="cm-number">5</span>];</span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><></span></span>
<span role="presentation"> </span>
<span role="presentation"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">>></span> <span class="cm-variable">ans</span>(<span class="cm-variable">r</span>,<span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span>(<span class="cm-variable">c</span>));<span class="cm-comment">//初始化r行c列的容器</span></span>
<span role="presentation"> <span class="cm-variable">v1</span>.<span class="cm-variable">size</span>()<span class="cm-comment">//v1有几行</span></span>
<span role="presentation"> <span class="cm-variable">v1</span>[<span class="cm-variable">i</span>].<span class="cm-variable">size</span>()<span class="cm-comment">//v1[i]几行有几个元素,即有几列</span></span>
<span role="presentation"></span>
<span role="presentation"> </span>
<span role="presentation"> <span class="cm-variable">遍历</span></span>
<span role="presentation"> <span class="cm-keyword">for</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">i</span> : <span class="cm-variable">v</span>)</span>
<span role="presentation"> <span class="cm-variable">cout</span> <span class="cm-operator"><<</span> <span class="cm-variable">i</span>;</span>
<span role="presentation"> <span class="cm-keyword">for</span>(<span class="cm-keyword">auto</span> <span class="cm-variable">i</span> <span class="cm-operator">=</span> <span class="cm-variable">v</span>.<span class="cm-variable">begin</span>() ; <span class="cm-variable">i</span> <span class="cm-operator"><</span> <span class="cm-variable">v</span>.<span class="cm-variable">end</span>() ; <span class="cm-variable">i</span><span class="cm-operator">++</span> ) </span>
<span role="presentation"> <span class="cm-variable">cout</span> <span class="cm-operator"><<</span> <span class="cm-operator">*</span><span class="cm-variable">i</span>;<span class="cm-comment">////auto 推荐使用;</span></span>
<span role="presentation"> <span class="cm-keyword">for</span>(<span class="cm-keyword">auto</span> <span class="cm-variable">i</span> <span class="cm-operator">=</span> <span class="cm-variable">v</span>.<span class="cm-variable">begin</span>() ; <span class="cm-variable">i</span> <span class="cm-variable">!</span><span class="cm-operator">=</span> <span class="cm-variable">v</span>.<span class="cm-variable">end</span>() ; <span class="cm-variable">i</span><span class="cm-operator">++</span> ) </span>
<span role="presentation"> <span class="cm-variable">cout</span> <span class="cm-operator"><<</span> <span class="cm-operator">*</span><span class="cm-variable">i</span>;</span>
<span role="presentation"> <span class="cm-keyword">for</span>(<span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span>::<span class="cm-variable">iterator</span> <span class="cm-variable">it</span> <span class="cm-operator">=</span> <span class="cm-variable">vecIntB</span>.<span class="cm-variable">begin</span>() ;<span class="cm-variable">it</span><span class="cm-operator">!=</span><span class="cm-variable">vecIntB</span>.<span class="cm-variable">end</span>();<span class="cm-variable">it</span><span class="cm-operator">++</span>) </span>
<span role="presentation"> <span class="cm-variable">cout</span><span class="cm-operator"><<*</span><span class="cm-variable">it</span>;</span>
<span role="presentation"> </span>
<span role="presentation"> <span class="cm-variable">删除</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">erase</span>(<span class="cm-variable">v</span>.<span class="cm-variable">begin</span>()<span class="cm-operator">+</span><span class="cm-number">2</span>)<span class="cm-comment">//删除第3个元素(v.begin()+2地址所在);</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">erase</span>(<span class="cm-variable">v</span>.<span class="cm-variable">begin</span>()<span class="cm-operator">+</span><span class="cm-number">2</span><span class="cm-variable">,v</span>.<span class="cm-variable">begin</span>()<span class="cm-operator">+</span><span class="cm-number">5</span>)<span class="cm-comment">//删除第3-5个元素(v.begin()+4地址所在)及以后所有元素;</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">erase</span>(<span class="cm-variable">v</span>.<span class="cm-variable">begin</span>(),<span class="cm-variable">v</span>.<span class="cm-variable">end</span>());<span class="cm-comment">//清空</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">clear</span>() ;<span class="cm-comment">//清空;</span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">begin</span>()<span class="cm-operator">+</span><span class="cm-number">1</span><span class="cm-comment">//允许这样操作 </span></span>
<span role="presentation"> <span class="cm-variable">v</span>.<span class="cm-variable">pop_back</span>() <span class="cm-operator">&</span> <span class="cm-variable">v</span>.<span class="cm-variable">erase</span>() <span class="cm-operator">&</span> <span class="cm-variable">v</span>.<span class="cm-variable">push_back</span> <span class="cm-variable">后</span> <span class="cm-variable">v</span>.<span class="cm-variable">size</span>() <span class="cm-variable">会变;</span></span>
<span role="presentation"> </span>
<span role="presentation"> <span class="cm-variable">注:姑且认为中v</span>(<span class="cm-variable">v</span>.<span class="cm-variable">begin</span>(),<span class="cm-variable">v</span>.<span class="cm-variable">end</span>()) <span class="cm-operator">=></span> <span class="cm-variable">v</span>(<span class="cm-variable">p1</span>,<span class="cm-variable">p2</span>),</span>
<span role="presentation"> <span class="cm-variable">p1为操作起始地址,p2为操作元素末地址</span><span class="cm-operator">+</span><span class="cm-number">1</span><span class="cm-variable">,</span> <span class="cm-variable">类似sort();</span></span>
<span role="presentation"> <span class="cm-variable">大概就是像操作p1</span><span class="cm-operator">-></span><span class="cm-variable">p2,需要中(p1,p2</span><span class="cm-operator">+</span><span class="cm-number">1</span><span class="cm-variable">)</span></span>
<span role="presentation"> <span class="cm-variable">(!!!此条仅为方便记忆,不保证说法的严谨,也欢迎大家指正</span>)</span>
<span role="presentation"> </span>
<span role="presentation"> </span>
<span role="presentation"> <span class="cm-variable">如有错误,欢迎指正,如有不足,欢迎补充。</span></span>
<span role="presentation">} </span>