### Checking if variables are pairwise distinct

If I want to check if all the variables are pairwise distinct, i.e. they're all different. Here's a code I wrote to do this:

 ``123`` ``````if (s!=e&&s!=n&&s!=d&&s!=m&&s!=o&&s!=r&&s!=y&&e!=n&&e!=d) if (e!=m&&e!=o&&e!=r&&e!=y&&n!=d&&n!=m&&n!=o&&n!=r&&n!=y) if (d!=m&&d!=o&&d!=r&&d!=y&&m!=o&&m!=r&&m!=y&&o!=r&&o!=y&&r!=y)``````

Is there perhaps some function that would let me do this in a lot shorter way, i.e. something like this (that would act the same way my other code does): `if (different(s,m,d,e,o,n,y))`

What is the best way you can think of? Without having all those variables in an array. Otherwise it wouldn't be so hard to create a function. thanks.
Last edited on
There is no such function in the standard library but you could easily create one yourself using variadic templates (C++11 feature).

 ``123456789`` ``````template bool different(const T& a, const T& b) { return a != b; } template bool different(const T& a, const T& b, Args const &... args) { return different(a, b) && different(a, args...) && different(b, args...); }``````
You could also just use a std::set.

 ``12345`` ``````set xs; xs.insert(s); xs.insert(m); ... if (xs.size() != 7)``````
Assuming this is school and variadic templates or stl is not an option here's something very simple:

 ``123456`` ``````#define diff2(a,b) ((a)==(b)) #define diff3(a,b,c) (diff2(a,diff2(b,c))) #define diff4(a,b,c,d) (diff2(a,diff3(b,c,d))) #define diff5(a,b,c,d,e) (diff2(a,diff4(b,c,d,e))) #define diff6(a,b,c,d,e,f) (diff2(a,diff5(b,c,d,e,f))) #define diff7(a,b,c,d,e,f,g) (diff2(a,diff6(b,c,d,e,f,g))) ``````
Stewbond, your given functions is not exactly what I need. Both the following codes output 0:

 ``123`` ``````int a,b,c; a=3; b=4; c=5; cout << diff3(a,b,c);``````

 ``123`` ``````int a,b,c; a=3; b=4; c=4; cout << diff3(a,b,c);``````

However, the former contains pairwise distinct variables while the latter does not. Perhaps there's a way of getting over this somehow?
Last edited on
 What is the best way you can think of? Without having all those variables in an array. Otherwise it wouldn't be so hard to create a function. thanks.

Is that approach really so wrong? =P

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````// http://ideone.com/DaYEEk #include #include #include #include #include template bool different(std::vector list) { if (list.size() > 1) { std::sort(list.begin(), list.end()); for (auto i = list.begin(); i != list.end() - 1; ++i) if (*i == *(i + 1)) return false; } return true; } template bool different(std::initializer_list list) { return different(std::vector(list.begin(), list.end())); } int main() { int a, b, c; a = 3; b = 4; c = 4; std::cout << std::boolalpha << different({ a, b, c }) << '\n' ; a = 5, b = 3, c = 4; std::cout << different({ a, b, c }) << '\n'; }``````
Duoas, I hadn't heard of std::set before and I'm not sure how should I go about using it. The compiler throws out the following error message:

 error: 'set' was not declared in this scope

That happened when all that was between 'int main ()' and 'return 0;' was your given code.
Last edited on
To use std::set you need to include <set>.
Duoas' method is very simple and it is the one I'll use, the other ones are a bit more complicated and not worth my effort. I appreciate all the answers here, thanks!
Last edited on
Topic archived. No new replies allowed.