This is just a small question, more aimed at understanding the usage of arrays than at solving a difficult problem.
I currently have an array of four integers (Neighbors) that I’d like to compare against a set of other arrays (which don’t exist anywhere else – I have no need to store them). I want to know which of the four arrays Neighbors is identical to. As somebody who doesn’t know any better, my first attempt was to do this:
if (Neighbors == {1, 1, 0, 0})
{
//code...
}
else if (Neighbors == {0, 1, 1, 0})
{
//code...
}
else if (Neighbors == {0, 0, 1, 1})
{
//code...
}
else if (Neighbors == {1, 0, 0, 1})
{
//code...
}
As you can see, the order of the integers is important. However, the above returned compiler errors about expecting primary expressions before curly-brace tokens.
So instead, I tried this:
int Sets[4][4] = { {1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}, {1, 0, 0, 1} };
if (Neighbors == Sets[0])
{
//code...
}
else if (Neighbors == Sets[1])
{
//code...
}
else if (Neighbors == Sets[2])
{
//code...
}
else if (Neighbors == Sets[3])
{
//code...
}
What happens here is that even when Neighbors = {0, 1, 1, 0} (for example), Neighbors == Sets[1] returns false.
Now, after doing this and wondering why, I remembered that the array variable is basically a pointer to the first element in a sequence. Right? So I think I get why the above code doesn’t work – I’m comparing two memory addresses, rather than two arrays. So instead I wrote this code, which works fine:
for (int ii = 0; ii < 4; ++ii)
{
bool Same = true;
for (int jj = 0; jj < 4; ++jj)
{
if (Neighbors[jj] != Set[ii][jj])
{
Same = false;
}
}
if (Same == true)
{
//code...
}
}
What I want to know is whether there is a way to compare arrays like this without going through two for-loops. It seems like something that ought to be simpler than this. I know a for-loop isn’t especially intensive when you’ve only got 4 values, but I still would have thought it’d be easier to determine whether two arrays contain identical information. If each array is a contiguous block of memory, I would have thought you could just look at those two blocks and check whether they are identical (which is basically what the for-loops are doing, although that requires doing it manually).
So is there a way to compare the content of arrays directly, preferably with a single line of code? If not, why not? I’d like to understand the science behind this issue.
You have tagged the question C++. Which means you should be using
std::vector. It has overloadedoperator==that does what you want (for two vectors).You can also use
std::equalorstd::lexicographical_comparefor anything you have iterators for, which includes primitive arrays.Of course you can also overload the
operator==for other things. Unfortunately you can’t overload it for primitive arrays, because overloading operators is only allowed if at least one argument is a class (or struct) type. But you could override it to compare vector with array. Something like:(this takes reference to array not degraded to pointer to check it’s declared size first and is therefore safe)
Of course all these methods have a loop hidden inside that compares the elements one by one. But you don’t have to write it.