fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. template <typename T>
  6. struct ChooseKfromN{
  7. T mbegin;
  8. T mend;
  9. std::vector<T> combo;
  10. ChooseKfromN(T first,T last,int k) : mbegin(first),mend(last),combo(k,first) {}
  11. bool increment(){
  12. for (auto it = combo.begin();it!=combo.end();it++){
  13. if (++(*it) == mend){
  14. if (it != combo.end()){
  15. auto next = it;
  16. next++;
  17. auto nexit = (*next);
  18. nexit++;
  19. std::fill(combo.begin(),next,nexit);
  20. }
  21. } else { return true;}
  22. }
  23. std::cout << "THIS IS NEVER REACHED FOR A MAP !! \n" << std::endl;
  24. return false;
  25. }
  26. typename std::vector<T>::const_iterator begin(){ return combo.begin();}
  27. typename std::vector<T>::const_iterator end() { return combo.end();}
  28. };
  29.  
  30. template <typename T>
  31. ChooseKfromN<T> createChooseKfromN(T first,T last,int k) { return ChooseKfromN<T>(first,last,k); }
  32.  
  33.  
  34. int main(){
  35. std::vector<std::string> xx = {"A","B","C"};
  36. //std::map<int,int> xx;
  37. //xx[1] = 1;
  38. //xx[2] = 2;
  39.  
  40. auto kn = createChooseKfromN(xx.begin(),xx.end(),2);
  41. int counter = 0;
  42. do {
  43. for (auto it = kn.begin();it != kn.end();it++){
  44. std::cout << (**it) << "\t";
  45. }
  46. std::cout << "\n";
  47. counter++;
  48. } while(kn.increment());
  49. std::cout << "counter = " << counter << "\n";
  50. }
  51.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
A	A	
B	A	
C	A	
B	B	
C	B	
C	C	
THIS IS NEVER REACHED FOR A MAP !! 

counter = 6