Skip to content

Instantly share code, notes, and snippets.

@HaiyangXu
Created December 27, 2014 10:57
Show Gist options
  • Save HaiyangXu/0744676407c3fecf9e40 to your computer and use it in GitHub Desktop.
Save HaiyangXu/0744676407c3fecf9e40 to your computer and use it in GitHub Desktop.
combination for cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
combination for get m from num of numbers
*/
class combination
{
public:
//m from num of numbers
combination(int num,int m):init(true)
{
//let bits as the permutation
bits=vector<int> (num,1);
fill(bits.begin(),bits.begin()+m,0);
}
// get next result seq while true;
bool next(vector<int> &seq)
{
seq.resize(0);
if(init)
{
getSeq(seq);
init =false;
return true;
}
bool rval=next_permutation(bits.begin(), bits.end());
getSeq(seq);
return rval;
}
private:
void getSeq(vector<int> &seq)
{
for(int i=0;i<bits.size();i++)
{
if(!bits[i])seq.push_back(i+1);
}
}
vector<int> bits;
bool init;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment