Is there a way to get the equality operators to work for comparing arrays of the same type?
For example:
int x[4] = {1,2,3,4};
int y[4] = {1,2,3,4};
int z[4] = {1,2,3,5};
if (x == y) cout << "It worked!"
I'm aware that as is, it's just comparing pointer values - but I was hoping there's some kind of typedef trick or something like that so it wouldn't need a loop or a memcmp call.
You can use standard equal algorithm
if (std::equal(x,x+4,y)) cout << "It worked!";
Use std::equal
as:
if(std::equal(x, x+ xsize, y)) std::cout << "equal";
It checks equality of elements in the same order. That means, according to std::equal
the following arrays are not equal.
int x[4] = {1,2,3,4};
int y[4] = {1,2,4,3}; //order changed!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With