Google is your friend, not sure what you searched for. I found this link with sample code -
C++ Find Element in Vector[
^]
Your code will look something like -
#include <iostream>
#include <vector>
template <typename T>
bool findElementInVector(const std::vector<T>& vec, const T& element) {
for (const T& item : vec) {
if (item == element) {
return true; }
}
return false; }
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
int target = 3;
if (findElementInVector(numbers, target)) {
std::cout << "Element " << target << " found in the vector." << std::endl;
} else {
std::cout << "Element " << target << " not found in the vector." << std::endl;
}
target = 6;
if (findElementInVector(numbers, target)) {
std::cout << "Element " << target << " found in the vector." << std::endl;
} else {
std::cout << "Element " << target << " not found in the vector." << std::endl;
}
return 0;
}
Using an online compiler like -
C++ Online Compiler[
^], I get an output of -
Element 3 found in the vector.
Element 6 not found in the vector