要在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"。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/829517.html