Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:25:02+00:00 2026-06-06T21:25:02+00:00

This is just a small question, more aimed at understanding the usage of arrays

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:25:04+00:00Added an answer on June 6, 2026 at 9:25 pm

    You have tagged the question C++. Which means you should be using std::vector. It has overloaded operator== that does what you want (for two vectors).

    You can also use std::equal or std::lexicographical_compare for 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:

    template<typename T, typename Alloc, size_t S>
    bool operator==(std::vector<T, Alloc> v, const T (&a)[S])
    {
        return v.size() == S && std::equal(v.begin(), v.end(), a);
    }
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning JavaFX and this is just a small programming question. I have 3
This is a bit more of a fun question than a serious one, but
I think this question is best asked with a small code snippet I just
I have a more general question but any advice in implementing this in C#
This is more of a PHP question than a WordPress question, but I am
Okay, before you guys go nuts -- this is just a small site, temporary
This just saves time. Since I already have a web applciation. I can just
I understand this is an easy question but for some reason this just isn't
This is a continuation on this question : The problem is simple. I need
I have tried to ask a variant of this question before. I got some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.