Created
December 27, 2014 10:57
-
-
Save HaiyangXu/0744676407c3fecf9e40 to your computer and use it in GitHub Desktop.
combination for cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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