Home > front end >  How to check whether two array or list are identical?
How to check whether two array or list are identical?

Time:01-10

In python, one can easily determine whether arr1 is identical to arr2, for example:

[1,2,3] == [1,2,3]
{1,2,3} == {3,1,2}
(1,2,3) == (1,2,3)

How do you do this in C ?

//#include <bits/stdc  .h>
using namespace std;
int main()
{
//    if ({1, 2}== {1, 2})
//        cout<<" {1, 2}== {1, 2}";
    if ((1, 2)== (2, 2))
        cout<<" (1, 2)== (1, 2) equal";
}

if ({1, 2}== {1, 2}) cout<<" {1, 2}== {1, 2}"; throws an error error: expected primary-expression before ‘{’ token, however, if ((1, 2)== (2, 2)) cout<<" (1, 2)== (1, 2) equal"; gives unexpected result, it thinks (1, 2) and (2, 2) are the same.

Do I have to convert list to vector do the comparison in C , like below?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    if (vector<int>({1, 2})== vector<int>({1, 2}))
        cout<<" vector<int>({1, 2})== vector<int>({1, 2}";

}

So what are the data type of (1,2) and {1,2} in C ?

CodePudding user response:

So what are the data type of (1,2) and {1,2} in c ?

(1,2) is an expression that uses the comma operator. The type of this expression is int.

{1,2} is an adventure into the shadowy parts of C . This construct itself is syntactically a braced initialization list, and it is not a formal type in of itself, of any kind. This always creates a bunch of sad faces whenever one wants to employ the services of forwarding references since braced initialization lists are not formal types, and cannot be squeezed into a forwarding reference.

But all hope is not lost. When used in the context shown here, before too much time elapses this construct quickly becomes a std::initializer_list, which is a very real type. And then, in the shown code, this gets passed to std::vector's overloaded constructor, which takes a std::initializer_list as its parameter.

CodePudding user response:

So what are the data type of (1,2) and {1,2} in c ?

{1,2} is an initializer list, you can't use it in a comparison like that but you can assign it, i.e:

auto a = {1, 2};

Though it's not of much use, unless you specify the type, for example:

std::vector a = {1, 2};

Here you have a vector of int with two elements 1 and 2.


(1, 2) == (2, 2) is really nothing, the element on the left of the comma will be discarded, it's as if you have 2 == 2.


Do I have to convert list to vector do the comparison in C , like below?

AFAICT, yes, a type must be involved for a proper comparison to take place.

If you're sure the arrays will have the same number of elements you can use is_permutation to assert if the arrays have the same elements in a different order:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    
    std::vector<int> v1 {1, 2, 3};
    std::vector<int> v2 {3, 1, 2};

    if(std::is_permutation(v1.begin(), v1.end(), v2.begin())){
      std::cout << "v1 == v2";
    }   
}

Alternatively you can use std::sort and then perform the comparison, provided you use an STL container, like a std::vector or an std::array for which overloads exist for the == operator.

CodePudding user response:

In python, one can easily determine whether arr1 is identical to arr2, for example:

[1,2,3] == [1,2,3]

In Python the square brackets are enough to denote a list. In C , which a statically typed language, you can't do this. You have to declare the arrays or vectors using their types:

std::vector<int> vec1 = {1, 2, 3};
std::vector<int> vec2 = {1, 2, 3};

As explained in the other answers, on it's own {1, 2, 3} is a braced initialization list, not an array or vector. In C , arrays and vectors do not have value semantics. You can't compare them like you would two integers using ==. Instead you would have to use a function, such as std::equal, from the standard library to compare the two:

std::equal(std::begin(vec1), std::end(vec1), std::begin(vec2), std::end(vec2))

which internally compares each element in the two vectors one by one. Of course, this is more long winded than in Python but under the hood Python is doing the same thing.

Alternatively, you can use std::array, which is a container that encapsulates fixed size arrays and that does have an operator ==:

std::array<int, 3>{1,2,3} == std::array<int, 3>{1,2,3}

For sets:

{1,2,3} == {3,1,2}

and tuples:

(1,2,3) == (1,2,3)

you can use the == operator like so:

std::set<int>{ 1, 2, 3 } == std::set<int>{3, 1, 2}
std::make_tuple( 1, 2, 3 ) == std::make_tuple(3, 1, 2)

but you have to explicitly create the sets and tuples first unlike Python, which infers from the brackets what you want to create.

Demo

  •  Tags:  
  • Related