c++中index函数如何查找元素

avatar
作者
猴君
阅读量:0

在C++中,std::vectorstd::array等容器类提供了operator[]来访问其元素。对于std::vectoroperator[]接受一个整数索引作为参数,并返回该索引处的元素。对于std::arrayoperator[]同样接受一个整数索引作为参数,并返回该索引处的元素。

以下是一个使用operator[]查找元素的示例:

#include <iostream> #include <vector>  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 << " is " << value << std::endl;     return 0;

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!