std::vector::operator[]
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| reference operator[]( size_type pos ); |
||
| const_reference operator[]( size_type pos ) const; |
||
返回一个引用的元素在指定的位置
pos。没有执行边界检查.Original:
Returns a reference to the element at specified location
pos. No bounds checking is performed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 参数
| pos | - | 该元素的位置返回
Original: position of the element to return The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 返回值
参考所需的元素
Original:
reference to the requested element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 复杂性
常数
Original:
Constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
下面的代码使用
operator[]读取和写入到std::vector<int>
Original:
The following code uses
operator[] read from and write to a std::vector<int>:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <vector> #include <iostream> int main() { std::vector<int> numbers {2, 4, 6, 8}; std::cout << "Second element: " << numbers[1] << '\n'; numbers[0] = 5; std::cout << "All numbers:"; for (auto i : numbers) { std::cout << ' ' << i; } std::cout << '\n'; }
Output:
Second element: 4 All numbers: 5 4 6 8
[编辑] 另请参阅
| 访问指定的元素与边界检查 Original: access specified element with bounds checking The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |