阅读量:88
在C++中,std::vector和std::array等容器类提供了operator[]来访问其元素。对于std::vector,operator[]接受一个整数索引作为参数,并返回该索引处的元素。对于std::array,operator[]同样接受一个整数索引作为参数,并返回该索引处的元素。
以下是一个使用operator[]查找元素的示例:
#include
#include
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
int index = 2;
int value = vec[index];
std::cout << "The value at index " << index class="hljs-string">" is " << value class="hljs-keyword">return 0;