• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[C] Arrays?

Santi

Theres no way im stopping
Joined
Aug 29, 2010
Messages
1,975
Reaction score
152
Location
00
How can i use LUA array in C?
LUA:
if isInArray({})
That function
Thankss
 
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;


}
 
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.
 
Last edited:
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;


}

@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
 
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.
 
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.

But I need to take out the i array,
i its a hipothetic number, since Im trying to make a function, but I need like this:
if i==x or if i==y or if i==z
No range, I want it so I can choose the numbers for x,y and z.
Like in LUA when you use array, you just put the numbers there(the numbers you want).
Probably im just wrong, but thats what I read from your script :p
Sorry for bothering, I feel so nub :D
 
As stop being silly fallen. He doesn't even know hwat's the diffrence between c and c++. And acroding to this script it's about template. So if i were you i'd shoot in my head withing a rocket lanucher. ;)

Btw fallen. Tonight i am sleeping on the top.
 
@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

Actually i still don't know , what u mean from your big comment , but if u mean u need it got multi types, then u can use this
Code:
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;
    }

This just loop through the array you put as param in the function to check if any of its value is equal to the value u put in the second param.

example? :

Code:
    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 false it would type that

You see i have compaired a string array with a string value, but u can compare that string array with a char or int, that is why the template function have 2 classes, like if you want directly to put the value in const character it is valid like
Code:
if (isInArray(a,"karim"))
 
Last edited:
@up you're failing C++.

1stly. Trying to compare 2 diffrent objects (might be the same) If they're not same type it's really not often that there exist == operator for them. But if it's same type, then there's no need in doing template for 2 classes.

next Thing. sizeof(a) returns you a size of whole array (tell me fallen if i am wrong) in bytes. it's not real size of array. If you really wants to do it then it'll be better to do "sizeof(a)/sizeof(T)"
 
wut the fuck is wrong with this?
[cpp]#include <string>
#include <iostream>
#include <assert.h>
#include <cstring>

template<typename _Tp>
class Array
{
public:
class iterator
{
public:
typedef iterator self_type;
typedef _Tp* pointer;
typedef _Tp& reference;

iterator(pointer ptr) : m_ptr(ptr) {}

self_type operator++() {
++m_ptr;
return *this;
}

reference operator*() { return *m_ptr; }
pointer operator->() { return m_ptr; }

bool operator==(const self_type& rhs) { return m_ptr == rhs.m_ptr; }
bool operator!=(const self_type& rhs) { return !(*this == rhs); }

private:
pointer m_ptr;
};

typedef iterator iterator;
Array(int size) : m_size(size) { m_data = new _Tp[m_size]; }

iterator begin()
{ return iterator(m_data); }

iterator end()
{ return iterator(m_data + m_size); }

_Tp& operator[](int pos) {
assert(m_size >= pos);
return m_data[pos];
}

size_t size() const { return m_size; }

private:
_Tp* m_data;
size_t m_size;
};

int main() {
typedef Array<int> array;
array a(3);
a[0] = 1;
a[1] = 2;
a[2] = 3;

//iterator way
for (array::iterator it = a.begin(); it != a.end(); ++it) {
if (*it == 1)
std::clog << *it << std::endl;
}

typedef Array<char*> array_;
array_ b(5);
b[0] = "Fallen";
b[1] = "Is";
b[2] = "Da";
b[3] = "Sexy";
b[4] = "Beast";

for (array_::iterator it = b.begin(); it != b.end(); ++it) {
if (*it && std::strcmp(*it, "Fallen") == 0)
std::clog << "Ok" << std::endl;
}

return 0;
}
[/cpp]
 
Back
Top