If the objective of your program is to find the determined value that is the correct way:
template <typename T>
bool index(std::vector<T>& vec, T value){
return std::any_of(vec.begin(), vec.end(), [value](T j) { return value == j; });
}
My program doesn't differ much from @caleth's answer as in order to write a general-purpose function that can handle various sorts of ranges, such as an std::array, std::vector, a C-like array, or others, you should write a template like this:
template <typename iter, typename T>
bool any_index(iter begin, iter end, T value) {
for (; begin != end; ++begin) {
if (static_cast<T>(*begin) == value) {
return true;
}
}
return false;
}
usage:
int main() {
vector<int> v1{ 4, 3, 2, 1 };
vector<string> v2{ "hey", "language", "compiler", "bug" };
vector<char> v3{ 'a', 'b','c', 'd' };
array<int, 6> c{ {1,2,3,4,5,6} };
unsigned char buf[5] = {0x11, 0x22, 0x33, 0x44, 0x55};
assert(any_index(begin(v1), end(v1), 4) == true);
assert(any_index(begin(v2), end(v2), string("compiler")) == true);
assert(any_index(begin(v3), end(v3), 'd') == true);
assert(any_index(begin(c), end(c), 5) == true);
assert(any_index(begin(buf), end(buf), 0x11) == true);
return 0;
}
Full example (godbolt)