Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Combinations in C++

, 13 Sep 2009 CPOL
An article on finding combinations.
// TestComb.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include "veccomb.h"

int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;
	typedef vector<char> Set;

     vector<Set> VecOrig;

     Set v1, v2, v3;
     v1.push_back('A');
     v1.push_back('B');
     v1.push_back('C');
     v1.push_back('D');
     v2.push_back('A');
     v2.push_back('B');
     v2.push_back('C');
     v3.push_back('A');
     v3.push_back('B');
     v3.push_back('C');
     VecOrig.push_back(v1);
     VecOrig.push_back(v2);
     VecOrig.push_back(v3);

     vector<Set> VecResults;

     VecResults.push_back( Set() );
     VecResults.push_back( Set() );
     VecResults.push_back( Set() );

     VecResults.at(0).push_back( v1.at(0) );
     VecResults.at(0).push_back( v1.at(1) );
     VecResults.at(0).push_back( v1.at(2) );
     VecResults.at(1).push_back( v2.at(0) );
     VecResults.at(1).push_back( v2.at(1) );
     VecResults.at(2).push_back( v2.at(0) );
     VecResults.at(2).push_back( v2.at(1) );

	 while(FindVecComb(
		VecOrig,
		VecResults ) )
	 {
		std::string str="";
		for( size_t i=0; i<VecResults.size(); ++i )
		{
			for( size_t j=0; j<VecResults.at(i).size(); ++j )
			{
				str+=VecResults.at(i).at(j);
			}
			str+=" | ";
		}
		cout<<str.c_str()<<endl;
	 }

	 return 0;
}

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
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 13 Sep 2009
Article Copyright 2003 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid