📄 all.html
字号:
<p>For example, in the following code, the first "cout" statement will display garbage, whereas the second statement will actually display the last element of the vector:</p> <pre class="example-code"> vector<int> v1; v1.push_back( 0 ); v1.push_back( 1 ); v1.push_back( 2 ); v1.push_back( 3 ); int bad_val = *(v1.end()); cout << "bad_val is " << bad_val << endl; int good_val = *(v1.end() - 1); cout << "good_val is " << good_val << endl;</pre> <p>The next example shows how <a href= "begin.html">begin</a>() and end() can be used to iterate through all of the members of a vector:</p> <pre class="example-code"> vector<int> v1( 5, 789 ); vector<int>::iterator it; for( it = v1.begin(); it != v1.end(); it++ ) { cout << *it << 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 <vector> 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<char> alphaVector; for( int i=0; i < 10; i++ ) { alphaVector.push_back( i + 65 ); } int size = alphaVector.size(); vector<char>::iterator startIterator; vector<char>::iterator tempIterator; for( int i=0; i < size; i++ ) { startIterator = alphaVector.begin(); alphaVector.erase( startIterator ); // Display the vector for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) { cout << *tempIterator; } cout << 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<char> alphaVector; for( int i=0; i < 10; i++ ) { alphaVector.push_back( i + 65 ); } // display the complete vector for( int i = 0; i < alphaVector.size(); i++ ) { cout << alphaVector[i]; } cout << 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 < alphaVector.size(); i++ ) { cout << alphaVector[i]; } cout << 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> (C++ Lists) <a href="../cpplist/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 <vector> <a href="../containers.html">TYPE</a>& front(); const <a href="../containers.html">TYPE</a>& front() const;</pre> <p>The front() function returns a reference to the first element of the vector, and runs in <a href="../complexity.html">constant time</a>.</p> <p>For example, the following code uses a vector and the <a href="../cppalgorithm/sort.html">sort() algorithm</a> to display the first word (in alphabetical order) entered by a user:</p> <pre class="example-code"> vector<string> words; string str; while( cin >> str ) words.push_back(str); sort( words.begin(), words.end() ); cout << "In alphabetical order, the first word is '" << words.front() << "'." << endl;</pre> <p>When provided with this input:</p> <pre class="example-code"> now is the time for all good men to come to the aid of their country</pre> <p>...the above code displays:</p> <pre class="example-code"> In alphabetical order, the first word is 'aid'.</pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="back.html">back</a><br> (C++ Lists) <a href="../cpplist/pop_front.html">pop_front</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"> insert </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> iterator insert( iterator loc, const <a href="../containers.html">TYPE</a>& val ); void insert( iterator loc, <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& val ); template<<a href="../containers.html">TYPE</a>> 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>Note that inserting elements into a vector can be relatively time-intensive, since the underlying data structure for a vector is an array. In order to insert data into an array, you might need to displace a lot of the elements of that array, and this can take <a href="../complexity.html">linear time</a>. If you are planning on doing a lot of insertions into your vector and you care about speed, you might be better off using a container that has a linked list as its underlying data structure (such as a <a href="../cpplist/index.html">List</a> or a <a href="../cppdeque/index.html">Deque</a>).</p> <p>For example, the following code uses the insert() function to splice four copies of the character 'C' into a vector of characters:</p> <pre class="example-code"> // Create a vector, load it with the first 10 characters of the alphabet vector<char> alphaVector; for( int i=0; i < 10; i++ ) { alphaVector.push_back( i + 65 ); } // Insert four C's into the vector vector<char>::iterator theIterator = alphaVector.begin(); alphaVector.insert( theIterator, 4, 'C' ); // Display the vector for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ ) { cout << *theIterator; } </pre> <p>This code would display:</p> <pre class="example-code"> CCCCABCDEFGHIJ </pre> <p>Here is another example of the insert() function. In this code, insert() is used to append the contents of one vector onto the end of another:</p> <pre class="example-code"> vector<int> v1; v1.push_back( 0 ); v1.push_back( 1 ); v1.push_back( 2 ); v1.push_back( 3 ); vector<int> v2; v2.push_back( 5 ); v2.push_back( 6 ); v2.push_back( 7 ); v2.push_back( 8 ); cout << "Before, v2 is: "; for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; v2.insert( v2.end(), v1.begin(), v1.end() ); cout << "After, v2 is: "; for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl;</pre> <p>When run, this code displays:</p> <pre class="example-code"> Before, v2 is: 5 6 7 8 After, v2 is: 5 6 7 8 0 1 2 3</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="push_back.html">push_back</a><br> (C++ Lists) <a href="../cpplist/merge.html">merge</a><br> (C++ Lists) <a href="../cpplist/push_front.html">push_front</a><br> (C++ Lists) <a href="../cpplist/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 <vector> <strong>size_type</strong> max_size() const;</pre> <p>The max_size() function returns the maximum number of elements that the vector can hold. The max_size() function should not be confused with the <a href="size.html">size</a>() or <a href= "capacity.html">capacity</a>() functions, which return the number of elements currently in the vector and the the number of elements that the vector 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"> pop_back </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> void pop_back();</pre> <p>The pop_back() function removes the last element of the vector.</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> (C++ Lists) <a href="../cpplist/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"> push_back </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> void push_back( const <a href="../containers.html">TYPE</a>& val );</pre> <p>The push_back() function appends <em>val</em> to the end of the vector.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -