1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <algorithm>
#include <vector>
bool IsInArray(int*, int, int);
bool IsInVector(std::vector<int>&, int);
int main()
{
const int num_to_find { 30 };
int myints[] = { 10, 20, 30, 40 };
std::cout << "Array element was ";
if (!IsInArray(myints, 4, num_to_find))
{
std::cout << "NOT ";
}
std::cout << "found.\n";
std::vector<int> myvector(myints, myints + 4);
std::cout << "Vector element was ";
if (!IsInVector(myvector, num_to_find))
{
std::cout << "NOT ";
}
std::cout << "found.\n";
}
bool IsInArray(int* arr, int size, int num)
{
int* p = std::find(arr, arr + size, num);
if (p != arr + size)
{
return true;
}
return false;
}
bool IsInVector(std::vector<int>& vec, int num)
{
std::vector<int>::iterator it = std::find(vec.begin(), vec.end(), num);
if (it != vec.end())
{
return true;
}
return false;
}
|