View Single Post
Old Aug 16th, 2006, 1:41 AM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Next time, post in the proper forum: "Software design and algorithms".
std::vector<std::vector<value> >
get_combinations(std::vector<value> values, unsigned m)
{
	std::vector<std::vector<value> > ret;
	unsigned n = values.size();
	std::vector<value>::const_iterator* iters = 
		new std::vector<value>::const_iterator[m];
	for(unsigned i=0;i<m;i++)
		iters[i] = values.begin() + i;
	while(true)
	{
		unsigned i;
		std::vector<value> tmp;
		for(unsigned j=0;j<m;j++)
			tmp.push_back(*(iters[j]));
		ret.push_back(tmp);
		
		for(i=m;i>0;i--)
		{
			if(++(iters[i-1]) != values.end() - (m - i))
				break;
		}
		if(!i)
			break;
		for(unsigned j = i;j<m;j++)
		{
			iters[j] = iters[j-1]+1;
		}
	}
	return ret;
}
It was hard work forcing my brain to solve this in a non-recursive way.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote