c++中怎么用vector查找某个值


在C++中,可以使用std::find函数在vector中查找某个特定值。下面是一个示例代码:

#include <iostream>#include <vector>#include <algorithm>int main() {std::vector<int> vec = {1, 2, 3, 4, 5};int value_to_find = 3;auto it = std::find(vec.begin(), vec.end(), value_to_find);if (it != vec.end()) {std::cout << "Value found at index " << it - vec.begin() << std::endl;} else {std::cout << "Value not found" << std::endl;}return 0;}

在这个示例中,我们首先创建一个包含整数的vector,并定义要查找的值为3。然后使用std::find函数在vector中查找这个值,如果找到则输出其索引,否则输出"value not found"。


上一篇:c语言结构体数组如何定义

下一篇:android登录注册跳转代码怎么写


c++ vector
Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
热门搜索