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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  <p>The end() function returns an iterator just past the end of the  list.</p>  <p>Note that before you can access the last element of the list using  an iterator that you get from a call to end(), you&#39;ll have to  decrement the iterator first.</p>  <p>For example, the following code uses <a href=  "begin.html">begin</a>() and end() to iterate through all of the  members of a vector:</p>  <pre class="example-code"> vector&lt;int&gt; v1( 5, 789 ); vector&lt;int&gt;::iterator it; for( it = v1.begin(); it != v1.end(); it++ ) {   cout &lt;&lt; *it &lt;&lt; endl; }              </pre>  <p>The iterator is initialized with a call to <a href=  "begin.html">begin</a>(). After the body of the loop has been  executed, the iterator is incremented and tested to see if it is  equal to the result of calling end(). Since end() returns an iterator  pointing to an element just after the last element of the vector, the  loop will only stop once all of the elements of the vector have been  displayed.</p>  <p>end() runs in <a href="../complexity.html">constant time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="begin.html">begin</a><br>    <a href="rbegin.html">rbegin</a><br>    <a href="rend.html">rend</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <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><hr>  <div class="name-format">    front  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  <a href="../containers.html">TYPE</a>&amp; front();  const <a href="../containers.html">TYPE</a>&amp; front() const;</pre>  <p>The front() function returns a reference to the first element of  the list, and runs in <a href="../complexity.html">constant  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="back.html">back</a><br>    <a href="pop_front.html">pop_front</a><br>    <a href="push_front.html">push_front</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    insert  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  iterator insert( iterator loc, const <a href="../containers.html">TYPE</a>&amp; val );  void insert( iterator loc, <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>&amp; val );  template&lt;<a href="../containers.html">TYPE</a>&gt; void insert( iterator loc, <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );</pre>  <p>The insert() function either:</p>  <ul>    <li>inserts <em>val</em> before <em>loc</em>, returning an iterator    to the element inserted,</li>    <li>inserts <em>num</em> copies of <em>val</em> before    <em>loc</em>, or</li>    <li>inserts the elements from <em>start</em> to <em>end</em> before    <em>loc</em>.</li>  </ul>  <p>For example:</p>  <pre class="example-code"> // Create a vector, load it with the first 10 characters of the alphabet vector&lt;char&gt; alphaVector; for( int i=0; i &lt; 10; i++ ) {   alphaVector.push_back( i + 65 ); }               // Insert four C&#39;s into the vector vector&lt;char&gt;::iterator theIterator = alphaVector.begin(); alphaVector.insert( theIterator, 4, &#39;C&#39; );              // Display the vector for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )    {   cout &lt;&lt; *theIterator; }              </pre>  <p>This code would display:</p>  <pre class="example-code"> CCCCABCDEFGHIJ         </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="assign.html">assign</a><br>    <a href="erase.html">erase</a><br>    <a href="merge.html">merge</a><br>    <a href="push_back.html">push_back</a><br>    <a href="push_front.html">push_front</a><br>    <a href="splice.html">splice</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    max_size  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  <strong>size_type</strong> max_size() const;</pre>  <p>The max_size() function returns the maximum number of elements  that the list can hold. The max_size() function should not be  confused with the <a href="size.html">size</a>() or (C++ Strings)  <a href="../cppstring/capacity.html">capacity</a>() functions, which  return the number of elements currently in the list and the the  number of elements that the list will be able to hold before more  memory will have to be allocated, respectively.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="size.html">size</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    merge  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  void merge( list &amp;lst );  void merge( list &amp;lst, BinPred compfunction );</pre>  <p>The function merge() merges the list with lst, producing a  combined list that is ordered with respect to the &lt; operator. If  compfunction is specified, then it is used as the comparison function  for the lists instead of &lt;.</p>  <p>merge() runs in <a href="../complexity.html">linear time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="container_operators.html">Container operators</a><br>    <a href="insert.html">insert</a><br>    <a href="splice.html">splice</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    pop_back  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  void pop_back();</pre>  <p>The pop_back() function removes the last element of the list.</p>  <p>pop_back() runs in <a href="../complexity.html">constant  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="back.html">back</a><br>    <a href="erase.html">erase</a><br>    <a href="pop_front.html">pop_front</a><br>    <a href="push_back.html">push_back</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    pop_front  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  void pop_front();</pre>  <p>The function pop_front() removes the first element of the  list.</p>  <p>The pop_front() function runs in <a href=  "../complexity.html">constant time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="erase.html">erase</a><br>    <a href="front.html">front</a><br>    <a href="pop_back.html">pop_back</a><br>    <a href="push_front.html">push_front</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    push_back  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;list&gt;  void push_back( const <a href="../containers.html">TYPE</a>&amp; val );</pre>  <p>The push_back() function appends <em>val</em> to the end of the  list.</p>  <p>For example, the following code puts 10 integers into a list:</p>  <pre class="example-code">   list&lt;int&gt; the_list;   for( int i = 0; i &lt; 10; i++ )     the_list.push_back( i );           </pre>  <p>When displayed, the resulting list would look like this:</p>  <pre class="example-code"> 0 1 2 3 4 5 6 7 8 9            </pre>  <p>push_back() runs in <a href="../complexity.html">constant  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="assign.html">assign</a><br>    <a href="insert.html">insert</a><br>    <a href="pop_back.html">pop_back</a><br>    <a href="push_front.html">push_front</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    push_front  </div>  <div class="syntax-name-format">

⌨️ 快捷键说明

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