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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  <p>For example, the following code puts 10 integers into a vector:</p>  <pre class="example-code">   vector&lt;int&gt; the_vector;   for( int i = 0; i &lt; 10; i++ ) {     the_vector.push_back( i );   }     </pre>  <p>When displayed, the resulting vector 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>    (C++ Lists) <a href="../cpplist/push_front.html">push_front</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    rbegin  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  <a href="../iterators.html">reverse_iterator</a> rbegin();  const_<a href="../iterators.html">reverse_iterator</a> rbegin() const;</pre>  <p>The rbegin() function returns a <a href=  "../iterators.html">reverse_iterator</a> to the end of the current  vector.</p>  <p>rbegin() 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="end.html">end</a><br>    <a href="rend.html">rend</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    rend  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  <a href="../iterators.html">reverse_iterator</a> rend();  const_<a href="../iterators.html">reverse_iterator</a> rend() const;</pre>  <p>The function rend() returns a <a href=  "../iterators.html">reverse_iterator</a> to the beginning of the  current vector.</p>  <p>rend() 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="end.html">end</a><br>    <a href="rbegin.html">rbegin</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    reserve  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  void reserve( <strong>size_type</strong> size );</pre>  <p>The reserve() function sets the capacity of the vector to at least  <em>size</em>.</p>  <p>reserve() runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="capacity.html">capacity</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    resize  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  void resize( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>&amp; val = <a href="../containers.html">TYPE</a>() );</pre>  <p>The function resize() changes the size of the vector to  <em>size</em>. If <em>val</em> is specified then any newly-created  elements will be initialized to have a value of <em>val</em>.</p>  <p>This function runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href=    "vector_constructors.html">Vector constructors    &amp; destructors</a><br>    <a href="capacity.html">capacity</a><br>    <a href="size.html">size</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    size  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  <strong>size_type</strong> size() const;</pre>  <p>The size() function returns the number of elements in the current  vector.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="capacity.html">capacity</a><br>    <a href="empty.html">empty</a><br>    (C++ Strings) <a href="../cppstring/length.html">length</a><br>    <a href="max_size.html">max_size</a><br>    <a href="resize.html">resize</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    swap  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  void swap( container&amp; from );</pre>  <p>The swap() function exchanges the elements of the current vector  with those of <em>from</em>. This function operates in <a href=  "../complexity.html">constant time</a>.</p>  <p>For example, the following code uses the swap() function to  exchange the contents of two vectors:</p>  <pre class="example-code">  vector<string> v1;  v1.push_back("I'm in v1!");  vector<string> v2;  v2.push_back("And I'm in v2!");  v1.swap(v2);  cout << "The first element in v1 is " << v1.front() << endl;  cout << "The first element in v2 is " << v2.front() << endl;</pre>  <p>The above code displays:</p>  <pre class="example-code">  The first element in v1 is And I'm in v2!  The first element in v2 is I'm in v1!</pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="vector_operators.html">= operator</a><br>    (C++ Lists) <a href="../cpplist/splice.html">splice</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    Vector constructors  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  vector();  vector( const vector&amp; c );  vector( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>&amp; val = <a href="../containers.html">TYPE</a>() );  vector( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );  ~vector();</pre>  <p>The default vector constructor takes no arguments, creates a new  instance of that vector.</p>  <p>The second constructor is a default copy constructor that can be  used to create a new vector that is a copy of the given vector  <em>c</em>.</p>  <p>The third constructor creates a vector with space for <em>num</em>  objects. If <em>val</em> is specified, each of those objects will be  given that value. For example, the following code creates a vector  consisting of five copies of the integer 42:</p>  <pre class="example-code"> vector&lt;int&gt; v1( 5, 42 );         </pre>  <p>The last constructor creates a vector 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 );               // only proceed if we find both numbers if( iter1 != v.end() &amp;&amp; iter2 != v.begin() ) {   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 vector should be  destroyed.</p>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    Vector operators  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&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;  vector operator=(const vector&amp; c2);  bool operator==(const vector&amp; c1, const vector&amp; c2);  bool operator!=(const vector&amp; c1, const vector&amp; c2);  bool operator&lt;(const vector&amp; c1, const vector&amp; c2);  bool operator&gt;(const vector&amp; c1, const vector&amp; c2);  bool operator&lt;=(const vector&amp; c1, const vector&amp; c2);  bool operator&gt;=(const vector&amp; c1, const vector&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 vector can be examined with the []  operator.</p>  <p>Performing a comparison or assigning one vector to another takes  <a href="../complexity.html">linear time</a>. The [] operator runs in  <a href="../complexity.html">constant time</a>.</p>  <p>Two vectors are equal if:</p>  <ol>    <li>Their size is the same, and</li>    <li>Each member in location i in one vector is equal to the the    member in location i in the other vector.</li>  </ol>  <p>Comparisons among vectors 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></body></html>

⌨️ 快捷键说明

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