2

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++?

cigien
  • 57,834
  • 11
  • 73
  • 112
Albert G Lieu
  • 891
  • 8
  • 16

3 Answers3

3

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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
2

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:

    std::initializer_list<int> init = {1, 2};

You can access the data using iterators, init.begin() will give you a pointer to the first element and you can go on from there, if you specify a type, it will give you more functionality, 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 must use arrays and 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.

If you are able to use other kinds of collections, my advice would be to use an std::set which will compare true whatever the order of insertion may be:

#include <iostream>
#include <set>

int main() {
    
    std::set<int> s1 {1, 2, 3};
    std::set<int> s2 {3, 1, 2};

    if(s1 == s2){
      std::cout << "s1 == s2";
    }   
}

You can access the full container library and its details here then you can choose the one which is more suited to your needs.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
2

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 using their types:

int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};

As explained in the other answers, on it's own {1, 2, 3} is a braced initialization list, not an array. In C++, arrays 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(arr1), std::end(arr1), std::begin(arr2), std::end(arr2))

which internally compares each element in the two arrays 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 the STL containers like std::vector and std::array, that do have an operator ==:

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

The difference between a std::vector and a std::array is that the latter is a fixed size array whereas the former is variable.

For sets:

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

and tuples:

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

you can also 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

jignatius
  • 6,304
  • 2
  • 15
  • 30