Comparing arrays

If I have 2 arrays, say array1 = {2, 2, 4, 5, 6} which has sorted array elements and array2 = {4, 2, 6, 2, 5} that has same elements as array1 but not in sorted condition. How can I compare that both arrays have same elements. I have written the following code which works in all conditions except when array1 has two same elements and array2 has all different elements.
I am extremely frustrated. Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
counter=0;
        for (i=0; i<5; i++)
        {
            for (int j=0; j<5; j++)
                if (array2[i] == array1[j])
                {
                    counter++;
                    array1[j]=0;
                }
        }

        if (counter==5)
            cout << "You Win !" << endl << endl;
        else
        {
            cout << "You Lose !" << endl;
            cout << "The array elements are: ";
            for (i=0; i<5; i++)
                cout << array1[i] << " ";
            cout << endl << endl;
        }
Last edited on
This is certainly great. I was wondering if there is any other way. Because I am very beginner in learning C++ and this permutation function is beyond my level of expertise in C++. Looks like you know a lot more than I do. Let me know if there is other way around.
The function is trivial to use, you should be able to learn by example without issue:
1
2
3
4
5
6
7
8
9
10
int arr1[] = {2, 2, 4, 5, 6};
int arr2[] = {4, 2, 6, 2, 5};
if(std::is_permutation(std::begin(arr1), std::end(arr1), std::begin(arr2)))
{
    //...
}
else
{
    //...
}
I tried this but sounds like my compiler is not supporting the begin and end functions. I am using codeblocks 13.12 compiler to run only the C++ source file.
I am getting:

#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
Last edited on
You need to enable C++11 for your CodeBlocks compiler - you should be able to do it in the project settings somewhere. I don't use CodeBlocks myself so you may want to use Google to search for how to enable C++11.
ok let me try. Thanks for all your help. Really appreciate it.
Thank you boss. I turned the feature on in my compiler and your suggestion worked like a charm without any error. This is cool. Learned something new. Thanks a lot. :)
Topic archived. No new replies allowed.