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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  <p>The last constructor creates a dequeue that is initialized to  contain the elements between <em>start</em> and <em>end</em>. For  example:</p>  <pre class="example-code"> // create a vector of random integers cout &lt;&lt; &quot;original vector: &quot;; vector&lt;int&gt; v; for( int i = 0; i &lt; 10; i++ ) {   int num = (int) rand() % 10;   cout &lt;&lt; num &lt;&lt; &quot; &quot;;   v.push_back( num ); } cout &lt;&lt; endl;             // find the first element of v that is even vector&lt;int&gt;::iterator iter1 = v.begin(); while( iter1 != v.end() &amp;&amp; *iter1 % 2 != 0 ) {   iter1++; }               // find the last element of v that is even vector&lt;int&gt;::iterator iter2 = v.end(); do {   iter2--; } while( iter2 != v.begin() &amp;&amp; *iter2 % 2 != 0 );               cout &lt;&lt; &quot;first even number: &quot; &lt;&lt; *iter1 &lt;&lt; &quot;, last even number: &quot; &lt;&lt; *iter2 &lt;&lt; endl;          cout &lt;&lt; &quot;new vector: &quot;; vector&lt;int&gt; v2( iter1, iter2 ); for( int i = 0; i &lt; v2.size(); i++ ) {   cout &lt;&lt; v2[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>  <p>When run, this code displays the following output:</p>  <pre class="example-code"> original vector: 1 9 7 9 2 7 2 1 9 8 first even number: 2, last even number: 8 new vector: 2 7 2 1 9          </pre>  <p>All of these constructors run in <a href=  "../complexity.html">linear time</a> except the first, which runs in  <a href="../complexity.html">constant time</a>.</p>  <p>The default destructor is called when the dequeue should be  destroyed.</p>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    Container operators  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;deque&gt;  <a href="../containers.html">TYPE</a>&amp; operator[]( <strong>size_type</strong> index );  const <a href="../containers.html">TYPE</a>&amp; operator[]( <strong>size_type</strong> index ) const;  container operator=(const container&amp; c2);  bool operator==(const container&amp; c1, const container&amp; c2);  bool operator!=(const container&amp; c1, const container&amp; c2);  bool operator&lt;(const container&amp; c1, const container&amp; c2);  bool operator&gt;(const container&amp; c1, const container&amp; c2);  bool operator&lt;=(const container&amp; c1, const container&amp; c2);  bool operator&gt;=(const container&amp; c1, const container&amp; c2);</pre>  <p>All of the C++ containers can be compared and assigned with the  standard comparison operators: ==, !=, &lt;=, &gt;=, &lt;, &gt;, and  =. Individual elements of a dequeue can be examined with the []  operator.</p>  <p>Performing a comparison or assigning one dequeue to another takes  <a href="../complexity.html">linear time</a>. The [] operator runs in  <a href="../complexity.html">constant time</a>.</p>  <p>Two `containers` are equal if:</p>  <ol>    <li>Their size is the same, and</li>    <li>Each member in location i in one dequeue is equal to the the    member in location i in the other dequeue.</li>  </ol>  <p>Comparisons among dequeues are done lexicographically.</p>  <p>For example, the following code uses the [] operator to access all  of the elements of a vector:</p>  <pre class="example-code"> vector&lt;int&gt; v( 5, 1 ); for( int i = 0; i &lt; v.size(); i++ ) {   cout &lt;&lt; &quot;Element &quot; &lt;&lt; i &lt;&lt; &quot; is &quot; &lt;&lt; v[i] &lt;&lt; endl; }              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="at.html">at</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    empty  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;deque&gt;  bool empty() const;</pre>  <p>The empty() function returns true if the dequeue has no elements,  false otherwise.</p>  <p>For example, the following code uses empty() as the stopping  condition on a (C/C++ Keywords) <a href=  "../keywords/while.html">while</a> loop to clear a dequeue and  display its contents in reverse order:</p>  <pre class="example-code"> vector&lt;int&gt; v; for( int i = 0; i &lt; 5; i++ ) {   v.push_back(i); } while( !v.empty() ) {   cout &lt;&lt; v.back() &lt;&lt; endl;   v.pop_back(); }              </pre>  <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">    end  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;deque&gt;  iterator end();  const_iterator end() const;</pre>  <p>The end() function returns an iterator just past the end of the  dequeue.</p>  <p>Note that before you can access the last element of the dequeue  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;deque&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>    (C++ Lists) <a href="../cpplist/remove.html">remove</a><br>    (C++ Lists) <a href="../cpplist/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;deque&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 dequeue, 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;deque&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         

⌨️ 快捷键说明

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