Checking if variables are pairwise distinct

Jul 23, 2014 at 10:17am
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:

1
2
3
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 Jul 23, 2014 at 10:18am
Jul 23, 2014 at 10:45am
There is no such function in the standard library but you could easily create one yourself using variadic templates (C++11 feature).

1
2
3
4
5
6
7
8
9
template <typename T>
bool different(const T& a, const T& b) {
    return a != b;
}

template <typename T, typename... Args>
bool different(const T& a, const T& b, Args const &... args) {
    return different(a, b) && different(a, args...) && different(b, args...);
}
Jul 23, 2014 at 2:48pm
You could also just use a std::set.

1
2
3
4
5
set<int> xs;
xs.insert(s);
xs.insert(m);
...
if (xs.size() != 7)
Jul 23, 2014 at 3:18pm
Assuming this is school and variadic templates or stl is not an option here's something very simple:

1
2
3
4
5
6
#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))) 
Jul 23, 2014 at 5:48pm
Stewbond, your given functions is not exactly what I need. Both the following codes output 0:

1
2
3
int a,b,c;
a=3; b=4; c=5;
cout << diff3(a,b,c);


1
2
3
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 Jul 23, 2014 at 5:49pm
Jul 23, 2014 at 6:23pm
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// http://ideone.com/DaYEEk
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <initializer_list>

template <typename T>
bool different(std::vector<T> 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 <typename T>
bool different(std::initializer_list<T> list)
{
    return different(std::vector<T>(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';
}
Jul 23, 2014 at 6:34pm
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 Jul 23, 2014 at 6:36pm
Jul 23, 2014 at 6:45pm
To use std::set you need to include <set>.
Jul 23, 2014 at 6:53pm
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 Jul 23, 2014 at 6:55pm
Topic archived. No new replies allowed.