Click here to Skip to main content
13,189,324 members (68,316 online)
Click here to Skip to main content

Stats

249.4K views
5.4K downloads
69 bookmarked
Posted 12 Apr 2016

Combinations in C++

, 12 Apr 2016
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

Cake Processor
Software Developer (Senior)
United States United States

Semi-retired from writing articles but may contribute tips from time to time.



IT Certifications


  • IT Infrastructure Library Foundational (ITIL v3)

  • Scrum Alliance Certified Scrum Master (CSM)

  • Certified Secure Software Lifecycle Professional (CSSLP)


You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171016.2 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid