Click here to Skip to main content
11,645,893 members (75,728 online)
Click here to Skip to main content
Add your own
alternative version

Combinations in C++

, 13 Sep 2009 CPOL 206.6K 4.7K 58
An article on finding combinations.
#include <vector>
#include "combination.h"


template<class T>
bool FindVecComb(
	std::vector< std::vector< T > >& VecN,
	std::vector< std::vector< T > >& VecR
	)
{
	if(VecR.size() == 0)
		return false;

	size_t nDepth = VecR.size()-1;
	bool NoMore = true;
	do
	{
		NoMore = stdcomb::next_combination(	
			VecN.at(nDepth).begin(), 
			VecN.at(nDepth).end(), 
			VecR.at(nDepth).begin(), 
			VecR.at(nDepth).end() );

		if(NoMore == false)
		{
			// Initailize as before
			for( size_t i=0; i<VecR.at(nDepth).size(); ++i)
			{
				VecR.at(nDepth).at(i) = VecN.at(nDepth).at(i);
			}
		}

		if( nDepth == 0 && NoMore == false)
			return false;

		--nDepth;
	}
	while(NoMore == false);

	return true;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shao Voon Wong
Software Developer
Singapore Singapore
IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • EC-Council Certified Secure Programmer (ECSP) .NET
  • EC-Council Certified Ethical Hacker (CEH)
  • EC-Council Certified Security Analyst (ECSA)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 13 Sep 2009
Article Copyright 2003 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid