int isInArray(int arr[],int value,int range){
for (int i = 0; i <= range ; i++)
{
if (arr[i] == value)
return 1;
}
return 0;
}
template<typename T>
int isInArray(T *array, int range, T &value)
{
for(int i = 0; i< range; i++)
if((T)(array+i*sizeof(T)) == value)
return i;
return -1;
}
int t = isInArray<string>(array, arraysSize, value);
Code:template<typename T> int isInArray(T *array, int range, T &value) { for(int i = 0; i< range; i++) if((T)(array+i*sizeof(T)) == value) return i; return -1; }
Something like that. Usage:
Code:int t = isInArray<string>(array, arraysSize, value);
Anyway i'd use a vectors.
I donno if this support C but ..
This is for int type if you want for a string or other i think it is easy to edit.
Code:int isInArray(int arr[],int value,int range){ for (int i = 0; i <= range ; i++) { if (arr[i] == value) return 1; } return 0; }
template<typename T>
bool isInArray(T *array, int range, T *lookingForArray, int rangeTheSecond)
{
for(int i = 0; i< range; i++)
for(int j = 0; j < rangeTheSecond; j++)
if((T)(array+i*sizeof(T)) == (T)(lookingForArray+j*sizeof(T)))
return true;
return false;
}
My one works for any type (as long as this type has overloaded == operator), doggy code has one small error. I'll let u figure it out urself.
Code:template<typename T> bool isInArray(T *array, int range, T *lookingForArray, int rangeTheSecond) { for(int i = 0; i< range; i++) for(int j = 0; j < rangeTheSecond; j++) if((T)(array+i*sizeof(T)) == (T)(lookingForArray+j*sizeof(T))) return true; return false; }
Checks if array contains any item for the second array.
@Doggy: Wouldnt that get from 0 to range(i put), and make an array on those numbers, and then just see if they have a value 1-100?
Im looking for something that, would only get some numbers in array (not from x number to y number).
And I think it should be backwards(I mean the array should contain values not "i", since i will be for checking
if i==x or if i==y or if i==z
Its probably okay, or easy to edit, but Im really a noob at this language, started yesterday with this basics.
Thanks anyway, rep for you doggy
@QuaS: I dont get yours a lot, but I think its similar to Doggy's
template <class T, class U>
bool isInArray (T *a, U b)
{
int f = sizeof(a);
for(int i = 0; i < f; i++)
{
if (a[i]== b)
return true;
}
return false;
}
string a[2] = {"karim","ahmed"}; // string you want to check in
string h = "karim"; // string value , that would be checked for in the string
if (isInArray(a,h)) // out function function(array,value to check)
cout<< "true" << endl; // if true it will type that
else
cout<< "false" << endl;
if (isInArray(a,"karim"))