vector怎么查找指定元素

   2025-02-04 3690
核心提示:要在vector中查找指定元素,可以使用std::find函数。以下是示例代码:#include iostream#include vector#include algorithmint m

要在vector中查找指定元素,可以使用std::find函数。以下是示例代码:

#include <iostream>#include <vector>#include <algorithm>int main() {    std::vector<int> vec = {1, 2, 3, 4, 5};    // 查找元素3    int target = 3;    auto it = std::find(vec.begin(), vec.end(), target);    if (it != vec.end()) {        std::cout << "找到了元素" << target << std::endl;    } else {        std::cout << "未找到元素" << target << std::endl;    }    return 0;}

上述代码中,使用std::find函数在vector中查找元素3。如果找到了该元素,则输出"找到了元素3";如果未找到,则输出"未找到元素3"。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言