Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing arrays with comparison operator
    primarykey
    data
    text
    <p>This is just a small question, more aimed at understanding the usage of arrays than at solving a difficult problem.</p> <p>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:</p> <pre><code>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... } </code></pre> <p>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.</p> <p>So instead, I tried this:</p> <pre><code>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... } </code></pre> <p>What happens here is that even when <code>Neighbors = {0, 1, 1, 0}</code> (for example), <code>Neighbors == Sets[1]</code> returns false.</p> <p>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:</p> <pre><code> for (int ii = 0; ii &lt; 4; ++ii) { bool Same = true; for (int jj = 0; jj &lt; 4; ++jj) { if (Neighbors[jj] != Set[ii][jj]) { Same = false; } } if (Same == true) { //code... } } </code></pre> <p>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).</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload