#include<iostream>
#include<vector>
using std::cout;
using std::endl;
using std::cin;
using std::vector;
vector<int>::iterator findvalue(vector<int>::iterator begin,vector<int>::iterator end,int input)
{
while (begin!=end)
{
if (*begin == input)
return begin;
begin++;
}
return end;
}
int main()
{
vector<int> a;
for (int i = 0; i < 5; i++)
{
int temp;
cin >> temp;
a.push_back(temp);
}
for (auto &temp : a)
cout << temp << " ";
cout << endl;
int input = 0;
cin >> input;
if (findvalue(a.begin(), a.end(), input)!=a.end())
cout << "The find value is:"<<*findvalue(a.begin(), a.end(), input);
else
cout << "No value here";
return 0;
}
本文介绍了一种使用C++标准模板库(STL)中的vector容器进行元素查找的方法。通过迭代器实现对vector中特定值的搜索,并在找到该值时返回对应的迭代器,若未找到则返回end迭代器。此外,文章还展示了如何使用cin从用户输入接收数据填充vector,以及如何遍历并显示vector中的所有元素。

470

被折叠的 条评论
为什么被折叠?



