Click here to Skip to main content
15,893,161 members
Articles / Programming Languages / C++

Combinations in C++, Part 2

Rate me:
Please Sign up or sign in to vote.
4.81/5 (30 votes)
12 Apr 2016CPOL15 min read 103.2K   2.3K   50  
Introduce 4 new algorithms on finding combinations
#include <vector>
#include <iostream>
#include "CombFromRepSet.h"

using namespace std;
using namespace stdcomb;


int main()
{
	// Initialize the set
	vector<unsigned int> svi;
	svi.push_back(0); // 0
	svi.push_back(1); // 1
	svi.push_back(2); // 2
	svi.push_back(3); // 3
	svi.push_back(3); // 4
	svi.push_back(3); // 5
	svi.push_back(4); // 6
	svi.push_back(5); // 7

	// Object to find the combinations from set with repeated elements
	CCombFromRepSet cfrs;

	cfrs.SetRepeatSetInfo( svi );
	
	// Set the size of Set and number elements of the combination
	const unsigned int SET = 8;
	const unsigned int COMB = 3;
	cfrs.SetSizes( SET, COMB );

	// Initialize the first combination vector
	vector<unsigned int> vi;
	for( unsigned int j=0; j<COMB; ++j )
		vi.push_back( j );

	// Set the first combination
	cfrs.SetFirstComb( vi );

	// Display the first combination
	int Cnt=0;
	{
		cout<<Cnt<<")";
		++Cnt;
		for( unsigned int i=0; i<vi.size(); ++i)
		{
			cout<<svi[vi[i]]<<",";
		}
		cout<<endl;
	}

	// Find and display the subsequent combinations
	while( cfrs.GetNextComb( vi ) )
	{
		cout<<Cnt<<")";
		++Cnt;
		for( unsigned int i=0; i<vi.size(); ++i)
		{
			cout<<svi[vi[i]]<<",";
		}
		cout<<endl;
	}

	system( "pause" );

	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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions