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:

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
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...);
}
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)
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))) 
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
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';
}
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.