int num[4] = {
4, 6, 9, 11
};
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
int main ()
{
int a[] = {3, 6, 8, 33};
int x = 8;
bool exists = std::find(std::begin(a), std::end(a), x) != std::end(a);
}
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
set<int> nums;
generate_n(inserter(nums, nums.end()), 42, []{ return rand() % 42; });
int n = 0; cin >> n;
cout << "\n"
<< ((nums.find(n) != nums.end()) ? "" : "Not ")
<< "Found"
<< endl;
}
А без цикла никак?
#include <iostream>
bool find(int a[], int sz, int value)
{
if(sz == -1)
return false;
else if(a[sz] == value)
return true;
else
return find(a, sz - 1, value);
}
int main()
{
int nums[] = { 4, 6, 9, 11 };
int i = 0;
std::cin >> i;
std::cout << "\n"
<< std::boolalpha
<< find(nums, sizeof(nums) / sizeof(int) - 1, i);
}