How to generate a subset of size n of a given array?

I am using following code to generate subsets of size n:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void AlgoMMPCbar::subs(const std::vector<unsigned int>& org, const std::vector<unsigned int>& pre, size_t k, size_t n, SubSets& c) 
{
	if (n <= 1) {
		for(size_t i = k; i < org.size(); i++){
			std::vector<unsigned int> v(pre);// instead of printing...
			v.push_back(org.at(i));
			c.push_back(v);
		}
	} else {
		size_t n1 = n - 1;
		for(size_t i = k; i != org.size() - n1; i++){   // 
			std::vector<unsigned int> s(pre);
			s.push_back(org.at(i));
			subs(org,s,i+1,n1,c);
		}
	}
}
void AlgoMMPCbar::computeSubSets(const std::vector<unsigned int>& org, size_t& n, SubSets& c){
	 c.clear(); // clear previous data

	std::vector<unsigned int> pre;
	pre.reserve(n+1); // for performance
        if (n==0)
          c.push_back(pre);
	else
        	subs(org,pre,0, n, c); 
}


In my program i am generating subsets of size n then using them for further test/processing. But i never need to test all these generated subsets (in worst case it will check them all). The main time consumption of the program is in subset generation. Now i want to transform the above functionality to generate subsets one by one (not all at once, so, i can stop any time).

Would you like share your expertise to transform the above functionality in a function like subset.next(), to save computational time.

Thanks in advance.
Last edited on
Topic archived. No new replies allowed.