📄 all.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>C++ Vectors</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Vectors</a> </div> <div class="name-format"> assign </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> void assign( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& val ); void assign( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );</pre> <p>The assign() function either gives the current vector the values from <em>start</em> to <em>end</em>, or gives it <em>num</em> copies of <em>val</em>.</p> <p>This function will destroy the previous contents of the vector.</p> <p>For example, the following code uses assign() to put 10 copies of the integer 42 into a vector:</p> <pre class="example-code"> vector<int> v; v.assign( 10, 42 ); for( int i = 0; i < v.size(); i++ ) { cout << v[i] << " "; } cout << endl; </pre> <p>The above code displays the following output:</p> <pre class="example-code"> 42 42 42 42 42 42 42 42 42 42 </pre> <p>The next example shows how assign() can be used to copy one vector to another:</p> <pre class="example-code"> vector<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } vector<int> v2; v2.assign( v1.begin(), v1.end() ); for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; </pre> <p>When run, the above code displays the following output:</p> <pre class="example-code"> 0 1 2 3 4 5 6 7 8 9 </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> (C++ Strings) <a href="../cppstring/assign1.html">assign</a><br> <a href="insert.html">insert</a><br> <a href="push_back.html">push_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"> at </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> <a href="../containers.html">TYPE</a>& at( <strong>size_type</strong> loc ); const <a href="../containers.html">TYPE</a>& at( <strong>size_type</strong> loc ) const;</pre> <p>The at() function returns a reference to the element in the vector at index <em>loc</em>. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector.</p> <p>For example, consider the following code:</p> <pre class="example-code"> 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 class="example-code"> 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> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="vector_operators.html">Vector operators</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> back </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> <a href="../containers.html">TYPE</a>& back(); const <a href="../containers.html">TYPE</a>& back() const;</pre> <p>The back() function returns a reference to the last element in the vector.</p> <p>For example:</p> <pre class="example-code"> 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 class="example-code"> The first element is 0 and the last element is 4 </pre> <p>The back() function runs in <a href="../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="front.html">front</a><br> <a href="pop_back.html">pop_back</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> begin </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> iterator begin(); const_iterator begin() const;</pre> <p>The function begin() returns an iterator 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 begin() to initialize an iterator that is used to traverse the elements of a vector:</p> <pre class="example-code"> vector<string> words; string str; while( cin >> str ) words.push_back(str); vector<string>::iterator iter; for( iter = words.begin(); iter != words.end(); iter++ ) { cout << *iter << endl; }</pre> <p>When given this input:</p> <pre class="example-code"> hey mickey you're so fine</pre> <p>...the above code produces the following output:</p> <pre class="example-code"> hey mickey you're so fine</pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="vector_operators.html">[] operator</a><br> <a href="at.html">at</a><br> <a href="end.html">end</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"> capacity </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> <strong>size_type</strong> capacity() const;</pre> <p>The capacity() function returns the number of elements that the vector can hold before it will need to allocate more space.</p> <p>For example, the following code uses two different methods to set the capacity of two vectors. One method passes an argument to the constructor that suggests an initial size, the other method calls the reserve function to achieve a similar goal:</p> <pre class="example-code"> vector<int> v1(10); cout << "The capacity of v1 is " << v1.capacity() << endl; vector<int> v2; v2.reserve(20); cout << "The capacity of v2 is " << v2.capacity() << endl; </pre> <p>When run, the above code produces the following output:</p> <pre class="example-code"> The capacity of v1 is 10 The capacity of v2 is 20 </pre> <p>C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the <a href= "reserve.html">reserve</a>() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.</p> <p>The capacity() function runs in <a href= "../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="reserve.html">reserve</a><br> <a href="resize.html">resize</a><br> <a href="size.html">size</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> clear </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <vector> void clear();</pre> <p>The function clear() deletes all of the elements in the vector.</p> <p>clear() runs in <a href="../complexity.html">linear time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="erase.html">erase</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 <vector> bool empty() const;</pre> <p>The empty() function returns true if the vector has no elements, false otherwise.</p> <p>For example, the following code uses empty() as the stopping condition on a <a href= "../keywords/while.html">while</a> loop to clear a vector and display its contents in reverse order:</p> <pre class="example-code"> 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> <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 <vector> iterator end(); const_iterator end() const;</pre> <p>The end() function returns an iterator just past the end of the vector.</p> <p>Note that before you can access the last element of the vector using an iterator that you get from a call to end(), you'll have to decrement the iterator first. This is because end() doesn't point to the end of the vector; it points <strong>just past the end of the vector</strong>.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -