#include <set>
#include <vector>
#include <functional>
#include <algorithm>
// take a set of values and pairs them up at random, without repeats.
template < typename T >
std::vector< std::pair<T,T> > make_unique_pairs( const std::set<T>& set )
{
std::vector< std::pair<T,T> > result ;
// make a vector of references to elements in the set
std::vector< std::reference_wrapper< const T > > seq( set.begin(), set.end() ) ;
// make a random permutation of the references
std::random_shuffle( std::begin(seq), std::end(seq) ) ;
// make pairs and place them into the result
for( std::size_t i = 0 ; i < seq.size() - 1 ; i += 2 )
result.emplace_back( seq[i], seq[i+1] ) ;
return result ;
}
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
std::srand( std::time(nullptr) ) ;
const std::set<int> values { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ;
for( auto pair : make_unique_pairs(values) )
std::cout << pair.first << ',' << pair.second << '\n' ;
}
I2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxhbGdvcml0aG0+CgovLyB0YWtlIGEgc2V0IG9mIHZhbHVlcyBhbmQgcGFpcnMgdGhlbSB1cCBhdCByYW5kb20sIHdpdGhvdXQgcmVwZWF0cy4KdGVtcGxhdGUgPCB0eXBlbmFtZSBUID4Kc3RkOjp2ZWN0b3I8IHN0ZDo6cGFpcjxULFQ+ID4gbWFrZV91bmlxdWVfcGFpcnMoIGNvbnN0IHN0ZDo6c2V0PFQ+JiBzZXQgKQp7CiAgICBzdGQ6OnZlY3Rvcjwgc3RkOjpwYWlyPFQsVD4gPiByZXN1bHQgOwoKICAgIC8vIG1ha2UgYSB2ZWN0b3Igb2YgcmVmZXJlbmNlcyB0byBlbGVtZW50cyBpbiB0aGUgc2V0CiAgICBzdGQ6OnZlY3Rvcjwgc3RkOjpyZWZlcmVuY2Vfd3JhcHBlcjwgY29uc3QgVCA+ID4gc2VxKCBzZXQuYmVnaW4oKSwgc2V0LmVuZCgpICkgOwoKICAgI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