📄 all.html
字号:
<p>For example, the following code puts 10 integers into a vector:</p> <pre class="example-code"> vector<int> the_vector; for( int i = 0; i < 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 <vector> <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 <vector> <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 <vector> 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 <vector> void resize( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& 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 & 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 <vector> <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 <vector> void swap( container& 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 <vector> vector(); vector( const vector& c ); vector( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& 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<int> 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 << "original vector: "; vector<int> v; for( int i = 0; i < 10; i++ ) { int num = (int) rand() % 10; cout << num << " "; v.push_back( num ); } cout << endl; // find the first element of v that is even vector<int>::iterator iter1 = v.begin(); while( iter1 != v.end() && *iter1 % 2 != 0 ) { iter1++; } // find the last element of v that is even vector<int>::iterator iter2 = v.end(); do { iter2--; } while( iter2 != v.begin() && *iter2 % 2 != 0 ); // only proceed if we find both numbers if( iter1 != v.end() && iter2 != v.begin() ) { cout << "first even number: " << *iter1 << ", last even number: " << *iter2 << endl; cout << "new vector: "; vector<int> v2( iter1, iter2 ); for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << 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 <vector> <a href="../containers.html">TYPE</a>& operator[]( <strong>size_type</strong> index ); const <a href="../containers.html">TYPE</a>& operator[]( <strong>size_type</strong> index ) const; vector operator=(const vector& c2); bool operator==(const vector& c1, const vector& c2); bool operator!=(const vector& c1, const vector& c2); bool operator<(const vector& c1, const vector& c2); bool operator>(const vector& c1, const vector& c2); bool operator<=(const vector& c1, const vector& c2); bool operator>=(const vector& c1, const vector& c2);</pre> <p>All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, 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<int> v( 5, 1 ); for( int i = 0; i < v.size(); i++ ) { cout << "Element " << i << " is " << v[i] << 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 + -