Suppose I have an array of objects. Without loss of generality, it could be an array of indices: $$a = (0, 1, 2, ..., n)$$ Now suppose I want to choose two pairs of elements of this array. The order is not of the matter inside pairs so for pairs itself.
For example if $$a = (1,2,3,4)$$ then all those combinations would be $$(1,2), (3,4)\\(2,3), (1,4)\\...$$ But combinations like $$(1,2), (3,4)\quad \text{and}\quad (4,3), (1,2)$$ should be considered as equivalent.
I am a little bit confused how to write compact and effective algorithm for my purpose. I feel that there is a way to do it for general case (not just for pairs) but I cannot find it. If you know, please share.
So far, here is my code.
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector< int > targetVector = { 1, 2, 3, 4, 5 };
std::vector< int > index_pair = { 2, 2, 1, 1, 0 };
int pair1, pair2;
do
{
//reset pairs to 00000
pair1 = pair2 = 0;
for ( int i = 0; i < (int)index_pair.size(); i++ )
{
if( index_pair[i] == 1 ) { pair1 += ( 1 << (i+1) ); }
if( index_pair[i] == 2 ) { pair2 += ( 1 << (i+1) ); }
}
if ( pair1 > pair2 )
{
for ( auto index : index_pair )
{
std::cout << index;
}
std::cout << "\n";
}
}while( prev_permutation( index_pair.begin(), index_pair.end() ) );
return 0;
}
Here is the output that seems to be right.
22110
22101
22011
21210
21201
21021
20211
20121
12210
12201
12021
10221
02211
02121
01221
(4,5)
from the array(1,2,3,4)
? Am I missing something, or is there a typo? \$\endgroup\$