How to efficiently compare list elements against themselves?

For example my list consists of 4 integers : 1,2,3,4.
Program flow sould go like this:

1 < 2 = true
1 < 3 = true
1 < 4 = true

2 < 1 = false
2 < 3 = true
2 < 4 = true

3 < 1 = false
3 < 2 = false
3 < 4 = true

4 < 1 = false
4 < 2 = false
4 < 3 = false

I can see a pattern here, but i cant figure it out.
Also as you can see i dont want to compare same list element against self.
All help is mutch appreciated.
Last edited on
closed account (48T7M4Gy)
The pattern is simple, take each element in turn and compare it with each other one (in turn).

That makes a nest of two loops.

It might help if you consider what the comparison is supposed to lead to, and what is the actual comparison <, <=, >, >=, ==, != or what? Presumably it is <
Last edited on
closed account (48bpfSEw)
can you please insert your code snip?
Also as you can see i dont want to compare same list element against self.
Depends on what 'same' means. You cannot know whether they are equal unless you compare them with ==.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int array[] = {1,2,3,4};
    size_t size = sizeof(array)/sizeof(int);
    
    for (size_t i = 0; i < size; i++)
        for(size_t j = 0; j < size; j++)
        {
            if (i != j)
                std::cout << i << " compared to " << j << '\n';
        }

    return 0;
}
0 compared to 1
0 compared to 2
0 compared to 3
1 compared to 0
1 compared to 2
1 compared to 3
2 compared to 0
2 compared to 1
2 compared to 3
3 compared to 0
3 compared to 1
3 compared to 2
Program ended with exit code: 0


PS comparison is between array[i] and array[j] but somebody can fix that, u get the idea.

1 compared to 2
1 compared to 3
1 compared to 4
2 compared to 1
2 compared to 3
2 compared to 4
3 compared to 1
3 compared to 2
3 compared to 4
4 compared to 1
4 compared to 2
4 compared to 3
Program ended with exit code: 0
Last edited on
closed account (48T7M4Gy)
Maybe leave out line 11?
closed account (48T7M4Gy)
Already fixed.
closed account (48bpfSEw)
there are three conditions : >, <, ==

1
2
3
int idiff = v1-v2;
int iresult = 0;
if (idiff <0) iresult = 1; else if (idiff >0) iResult = -1; 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <functional>
#include <list>
#include <cstring>

const auto cmp_result = [] ( const auto& a, const auto& b, const auto& cmp ) -> std::string
{ return cmp(a,b) ? " < " : cmp(b,a) ? " > " : " == " ; } ; // invariant: LessThanComparable (strict weak ordering)

template < typename T, typename RANGE, typename CMP >
void print_cmp_result( const T& value, const RANGE& range, const CMP& cmp )
{ for( const auto& item : range ) std::cout << value << cmp_result( value, item, cmp ) << item << '\n' ; }

template < typename RANGE, typename CMP = std::less<> > void print_cmp_result( const RANGE& range, const CMP& cmp = {} )
{ for( const auto& item : range ) print_cmp_result( item, range, cmp ) ; }

int main()
{
    std::list<int> seq { 1, 2, 1, 3, 4 } ;
    print_cmp_result(seq) ;

    const char* const cstr[] = { "abcd", "efgh", "abc", "abcd", "ijk" } ;
    print_cmp_result( cstr, [] ( auto a, auto b ) { return std::strcmp(a,b) < 0 ; } ) ;
}

http://coliru.stacked-crooked.com/a/f9cc8a41bb5e35c9
Thank you kemort & gentleguy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int array_1i64[] = {1,2,3,4,5};
    size_t size = sizeof(array_1i64) / sizeof(int);

    for (size_t i = 0; i < size; i++)
        for(size_t j = 0; j < size; j++)
        {
            if (i != j)
                std::cout << array_1i64[i] << " < " << array_1i64[j] << " = " << ((array_1i64[i] < array_1i64[j]) ? "true" : "false") << '\n';
        }

    return 0;
}

Also thank you to JLBorges.
Topic archived. No new replies allowed.