⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 erase.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <meta name="generator" content=  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">  <title>erase</title>  <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table>  <tr>  <td>  <div class="body-content">  <div class="header-box">    <a href="../index.html">cppreference.com</a> &gt; <a href=    "index.html">C++ Lists</a> &gt; <a href="erase.html">erase</a>  </div>  <div class="name-format">    erase  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  iterator erase( iterator loc );  iterator erase( iterator start, iterator end );</pre>  <p>The erase() function either deletes the element at location  <em>loc</em>, or deletes the elements between <em>start</em> and  <em>end</em> (including <em>start</em> but not including  <em>end</em>). The return value is the element after the last element  erased.</p>  <p>The first version of erase (the version that deletes a single  element at location <em>loc</em>) runs in <a href=  "../complexity.html">constant time</a> for lists and <a href=  "../complexity.html">linear time</a> for vectors, dequeues, and  strings. The multiple-element version of erase always takes <a href=  "../complexity.html">linear time</a>.</p>  <p>For example:</p>  <pre class="example-code"> // Create a vector, load it with the first ten characters of the alphabet vector&lt;char&gt; alphaVector; for( int i=0; i &lt; 10; i++ ) {   alphaVector.push_back( i + 65 ); } int size = alphaVector.size(); vector&lt;char&gt;::iterator startIterator; vector&lt;char&gt;::iterator tempIterator; for( int i=0; i &lt; size; i++ ) {   startIterator = alphaVector.begin();   alphaVector.erase( startIterator );   // Display the vector   for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) {     cout &lt;&lt; *tempIterator;   }   cout &lt;&lt; endl; }              </pre>  <p>That code would display the following output:</p>  <pre class="example-code"> BCDEFGHIJ CDEFGHIJ DEFGHIJ EFGHIJ FGHIJ GHIJ HIJ IJ J              </pre>  <p>In the next example, erase() is called with two iterators to  delete a range of elements from a vector:</p>  <pre class="example-code"> // create a vector, load it with the first ten characters of the alphabet vector&lt;char&gt; alphaVector; for( int i=0; i &lt; 10; i++ ) {   alphaVector.push_back( i + 65 ); } // display the complete vector for( int i = 0; i &lt; alphaVector.size(); i++ ) {   cout &lt;&lt; alphaVector[i]; } cout &lt;&lt; endl;             // use erase to remove all but the first two and last three elements // of the vector alphaVector.erase( alphaVector.begin()+2, alphaVector.end()-3 ); // display the modified vector for( int i = 0; i &lt; alphaVector.size(); i++ ) {   cout &lt;&lt; alphaVector[i]; } cout &lt;&lt; endl;            </pre>  <p>When run, the above code displays:</p>  <pre class="example-code"> ABCDEFGHIJ ABHIJ          </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="clear.html">clear</a><br>    <a href="insert.html">insert</a><br>    <a href="pop_back.html">pop_back</a><br>    <a href="pop_front.html">pop_front</a><br>    <a href="remove.html">remove</a><br>    <a href="remove_if.html">remove_if</a>  </div>  </div>  </td>    </tr>  </table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -