-2

So, I need to find unique quadruples in C++. Any idea would help

Input 1 : [1, 0, 2, 3], [2, 0, 1, 3], [4, 5, 6, 7], [8, 9, 10, 11]

Output 1 : [2, 0, 1, 3], [4, 5, 6, ,7], [8, 9, 10, 11]

As [1,0,2,3] and [2,0,1,3] both contain same elements so either one can be in the output

Input 2 : [2, 0, 1, 3], [4, 5, 6, ,7], [8, 9, 10, 11], [15,16,17,18]

Output 2 : [2, 0, 1, 3], [4, 5, 6, ,7], [8, 9, 10, 11], [15,16,17,18]

I cannot initalize set (int,int,int,int). Any idea on how to get unique ones?


Update for people who asked for defining the question more: A quadruple is a combination of 4 integers for the problem. Problem states to find unique quadruples from all the given quadruples. A quadruple (a,b,c,d) is unique , if no other quadruple exists with all the elements same as this one, i.e. any quadruple formed from the permutation of (a,b,c,d) is not unique. Quadruples (a,b,c,d) and (a,d,b,c) are the same, where as quadruples (a,b,c,d) and (a,e,f,b) are not. Quadruples are unique if they contain atleast 1 element which is not common to both.

pranavhd
  • 25
  • 7
  • 1
    Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Mar 07 '18 at 09:50
  • 2
    Just use `std::unique()` with a binary predicate that treats two "quadruples" as equal if they contain the "same elements". – Peter Mar 07 '18 at 09:52
  • 1
    @Peter Must sort elements before calling `std::unique` – john Mar 07 '18 at 10:00
  • What does unique pair of quadruplets mean? I have problem with the word `pair` in your assignment – Killzone Kid Mar 07 '18 at 10:01
  • I understand the problem. Do you have one of those teachers that restricts which C++ and STL features you can use? If so, how are you hampered? Next, show us what you have done so far. Post a [MCVE]. – Jive Dadson Mar 07 '18 at 11:02
  • Could you post some code? – Jeroen Mar 07 '18 at 11:25
  • @JiveDadson, well I was doing on this page : https://ideone.com/QoOMWG, trying to create a set and adding, but there is some problem – pranavhd Mar 08 '18 at 02:41

1 Answers1

3

Write a comparator that sorts the integers in the quadruples before comparing them.

struct CompareQuads
{
    bool operator()(Quad x, Quad y) const
    {
        // sort x integers
        ...
        // sort y integers
        ...
        // return true if x < y (lexicographically)
        ...
    }
};

Use the comparator in std::set to eliminate duplicates.

std::set<Quad, CompareQuads> s;

Add all the quads to s and the duplicates will be removed. Iterate through s and print the ones that remain.

john
  • 85,011
  • 4
  • 57
  • 81
  • I was thinking of defining some kind of hash function on all numbers in a set but this solution is almost certainly more readable and faster. – Robinson Mar 07 '18 at 09:55
  • how exactly can we add quads to set? I'm sorry I cant figure out from the documentation http://www.cplusplus.com/reference/set/set/ – pranavhd Mar 07 '18 at 10:03
  • @pranavhd `s.insert(quad);` – john Mar 07 '18 at 10:09
  • okay, im feeling stupid now, i will be thankful though https://ideone.com/QoOMWG – pranavhd Mar 07 '18 at 10:20
  • @pranavhd Like I said you need `std::set s;`. std::set won't remove duplicates by magic you have to tell it what a duplicate. – john Mar 07 '18 at 10:59