📄 cppvector_details.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta name="generator" content="HTML Tidy for Linux/x86 (vers 1st October 2002), see www.w3.org"> <title>C++ Vectors</title> </head> <body bgcolor="#ffffff"> <table width="100%" bgcolor="#eeeeff"> <tr> <td><a href="index.html">cppreference.com</a> -> <a href="cppvector.html">C++ Vectors</a> -> Details</td> </tr> </table> <h1>C++ Vectors</h1> <hr> <h2><a name="Constructors">Constructors</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> vector(); vector( size_type num ); vector( size_type num, const <a href="containers.html">TYPE</a> &val ); vector( const vector &from ); vector( <a href="iterators.html">input_iterator</a> start, <a href="iterators.html">input_iterator</a> end );</pre> </td> </tr> </table> <p>C++ Vectors can be constructed with either:</p> <ul> <li>Nothing - which creates an empty vector,</li> <li><i>num</i> - creates an empty vector with space for <i>num</i> objects,</li> <li><i>num</i> and <i>val</i> - puts <i>num</i> copies of <i>val</i> in the vector,</li> <li><i>from</i> - creates a vector with the same contents as <i>from</i>, or</li> <li><i>start</i> and <i>end</i> - which creates a vector containing elements from <i>start</i> to <i>end</i>.</li> </ul> For example, the following example constructs a vector consisting of five copies of the integer 42. <pre>vector<int> v1( 5, 42 ); </pre> <hr> <h2><a name="Operators">Operators</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> v1 == v2 v1 != v2 v1 <= v2 v1 >= v2 v1 < v2 v1 > v2 v[]</pre> </td> </tr> </table> <p>The C++ Vectors can be compared using the standard operators: ==, !=, <=, >=, <, and >. Individual elements of a vector can be examined with the [] operator.</p> <p>Two vectors are equal if</p> <ol> <li>their size is the same, and</li> <li>each member in location <i>i</i> in one vector is equal to the the member in location <i>i</i> in the other vector.</li> </ol> Comparisons among vectors are done lexicographically. <br> <br> <i>Related topics:</i><br> <strong><a href="#at">at()</a>.</strong> <hr> <h2><a name="assign">assign</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void assign( <a href="iterators.html">input_iterator</a> start, <a href="iterators.html">input_iterator</a> end ); void assign( size_type num, const <a href="containers.html">TYPE</a> &val );</pre> </td> </tr> </table> <p>The assign() function either gives the current vector the values from <i>start</i> to <i>end</i>, or gives it <i>num</i> copies of <i>val</i>. This function will destroy the previous contents of the vector.</p> <hr> <h2><a name="at">at</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="containers.html">TYPE</a>& at( size_type loc );</pre> </td> </tr> </table> <p>The at() function returns a reference to the element in the current vector at <i>loc</i>. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector. For example, consider the following code:</p><pre>vector<int> v( 5, 1 );for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v[i] << endl;} </pre> <p>This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:</p><pre>vector<int> v( 5, 1 );for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v.at(i) << endl;} </pre> <p>Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.</p> <i>Related topics:</i><br> <strong><a href="#Operators">The [] Operator</a></strong> <hr> <h2><a name="back">back</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="containers.html">TYPE</a>& back();</pre> </td> </tr> </table> <p>The back() function returns a reference to the last element in the current vector. For example:</p><pre>vector<int> v;for( int i = 0; i < 5; i++ ) { v.push_back(i);}cout << "The first element is " << v.front() << " and the last element is " << v.back() << endl; </pre> <p>This code produces the following output:</p><pre>The first element is 0 and the last element is 4 </pre> <i>Related topics:</i><br> <strong><a href="#front">front().</a></strong> <hr> <h2><a name="begin">begin</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> begin();</pre> </td> </tr> </table> <p>The function begin() returns an <a href="iterators.html">iterator</a> to the first element in the current vector. For example, the following code uses an iterator to display all of the elements in a vector:</p><pre>vector<int> v1( 5, 789 );vector<int>::iterator it;for( it = v1.begin(); it != v1.end(); it++ ) cout << *it << endl;</pre> <i>Related topics:</i><br> <strong><a href="#end">end().</a></strong> <hr> <h2><a name="capacity">capacity</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type capacity();</pre> </td> </tr> </table> <p>The capacity() function returns the number of elements that the current vector can hold before it will need to allocate more space.</p> <hr> <h2><a name="clear">clear</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void clear();</pre> </td> </tr> </table> <p>The function clear() deletes all of the elements in the current vector.</p> <hr> <h2><a name="empty">empty</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> bool empty();</pre> </td> </tr> </table> <p>The empty() function returns <strong>true</strong> if the current vector has no elements, <strong>false</strong> otherwise. For example, the following code clears a given vector and displays its contents in reverse order:</p><pre>vector<int> v;for( int i = 0; i < 5; i++ ) { v.push_back(i);}while( !v.empty() ) { cout << v.back() << endl; v.pop_back();} </pre> <i>Related topics:</i><br> <strong><a href="#size">size()</a></strong> <hr> <h2><a name="end">end</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> end();</pre> </td> </tr> </table> <p>The end() function returns an <a href="iterators.html">iterator</a> just past the end of the current vector. Note that before you can access the last element of the vector, you'll have to decrement the iterator.</p> <i>Related topics:</i><br> <strong><a href="#begin">begin()</a></strong> <hr> <h2><a name="erase">erase</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> erase( <a href="iterators.html">iterator</a> loc ); <a href="iterators.html">iterator</a> erase( <a href="iterators.html">iterator</a> start, <a href="iterators.html">iterator</a> end );</pre> </td> </tr> </table> <p>The erase() function either deletes the element at location <i>loc</i>, or deletes the elements between <i>start</i> and <i>end</i>. The return value is the element after the last element erased. For example:</p><pre>// Create a vector, load it with the first ten characters of the alphabetvector<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>BCDEFGHIJCDEFGHIJDEFGHIJEFGHIJFGHIJGHIJHIJIJJ</pre> <i>Related topics:</i><br> <strong><a href="#pop_back">pop_back()</a>.</strong> <hr> <h2><a name="front">front</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="containers.html">TYPE</a>& front();</pre> </td> </tr> </table> <p>The front() function returns a reference to the first element of the current vector.</p> <i>Related topics:</i><br> <strong><a href="#back">back()</a>.</strong> <hr> <h2><a name="get_allocator">get_allocator</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> allocator_type get_allocator();</pre> </td> </tr> </table> <p>The function get_allocator() returns the current vector's allocator.</p> <hr> <h2><a name="insert">insert</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">iterator</a> insert( <a href="iterators.html">iterator</a> loc, const <a href="containers.html">TYPE</a> &val ); void insert( <a href="iterators.html">iterator</a> loc, size_type num, const <a href="containers.html">TYPE</a> &val ); void insert( <a href="iterators.html">iterator</a> loc, <a href="iterators.html">input_iterator</a> start, <a href="iterators.html">input_iterator</a> end );</pre> </td> </tr> </table> <p>The function insert() either:</p> <ul> <li>inserts <i>val</i> before <i>loc</i>, returning an <a href="iterators.html">iterator</a> to that element,</li> <li>inserts <i>num</i> copies of <i>val</i> before <i>loc</i>, or</li> <li>inserts the elements from <i>start</i> to <i>end</i> before <i>loc</i>.</li> </ul> For example: <pre>// Create a vector, load it with the first 10 characters of the alphabetvector<char> alphaVector;for( int i=0; i < 10; i++ ) alphaVector.push_back( i + 65 );// Insert four C's into the vectorvector<char>::iterator theIterator = alphaVector.begin();alphaVector.insert( theIterator, 4, 'C' );// Display the vectorfor( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ ) cout << *theIterator;</pre> <p>This code would display:</p><pre>CCCCABCDEFGHIJ</pre> <hr> <h2><a name="max_size">max_size</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type max_size();</pre> </td> </tr> </table> <p>The max_size() function returns the maximum number of elements that the current vector can hold.</p> <hr> <h2><a name="pop_back">pop_back</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void pop_back();</pre> </td> </tr> </table> <p>The function pop_back() deletes the last element in the current vector. For example:</p><pre>vector<char> alphaVector;for( int i=0; i < 10; i++ ) alphaVector.push_back( i + 65 );int size = alphaVector.size();vector<char>::iterator theIterator;for( int i=0; i < size; i++ ) { alphaVector.pop_back(); for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ ) cout << *theIterator; cout << endl;}</pre> <p>This code displays the following output:</p><pre>ABCDEFGHIABCDEFGHABCDEFGABCDEFABCDEABCDABCABA</pre> <i>Related topics:</i><br> <strong><a href="#erase">erase()</a>.</strong> <hr> <h2><a name="push_back">push_back</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void push_back( const <a href="containers.html">TYPE</a> &val );</pre> </td> </tr> </table> <p>The push_back() function appends <i>val</i> to the end of the current vector.</p> <hr> <h2><a name="rbegin">rbegin</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">reverse_iterator</a> rbegin();</pre> </td> </tr> </table> <p>The rbegin() function returns a reverse <a href="iterators.html">iterator</a> to the end of the current vector.</p> <hr> <h2><a name="rend">rend</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> <a href="iterators.html">reverse_iterator</a> rend();</pre> </td> </tr> </table> <p>The function rend() returns a reverse <a href="iterators.html">iterator</a> to the beginning of the current vector.</p> <hr> <h2><a name="reserve">reserve</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void reserve( size_type size );</pre> </td> </tr> </table> <p>The reserve() function sets the capacity of the current vector to at least <i>size</i>.</p> <hr> <h2><a name="resize">resize</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void resize( size_type size, <a href="containers.html">TYPE</a> val );</pre> </td> </tr> </table> <p>The function resize() changes the size of the current vector to <i>size</i>, setting any newly-created elements to <i>val</i>.</p> <hr> <h2><a name="size">size</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> size_type size();</pre> </td> </tr> </table> <p>The size() function returns the number of elements in the current vector.</p> <i>Related topics:</i><br> <strong><a href="#empty">empty()</a></strong> <hr> <h2><a name="swap">swap</a></h2> <i>Syntax:</i> <table bgcolor="#ccccff"> <tr> <td><pre> void swap( vector &from );</pre> </td> </tr> </table> <p>The swap() function exchanges the elements of the current vector with those of <i>from</i>.</p> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -