📄 at.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>at</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> > <a href="at.html">at</a> </div> <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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -