Click here to Skip to main content
15,895,746 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.3K   2.3K   50  
Introduce 4 new algorithms on finding combinations
// IntComb.cpp : Defines the entry point for the console application.
//

#include "IndexCombination.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace stdcomb;


int main(int argc, char* argv[])
{
	CIdxComb cb;

	cb.SetSizes(5,3);


	vector<string> vsAnimal;
	vsAnimal.push_back( "Elephant" );
	vsAnimal.push_back( "Deer" );
	vsAnimal.push_back( "Cat" );
	vsAnimal.push_back( "Bear" );
	vsAnimal.push_back( "Ape" );

	vector<unsigned int> vi(3);

	vi[0] = 0;
	vi[1] = 1;
	vi[2] = 2;

	cout<< vsAnimal[ vi[0] ] << " " 
		<< vsAnimal[ vi[1] ] << " " 
		<< vsAnimal[ vi[2] ] << "\n";

	int Total = 1;
	while ( cb.GetNextComb( vi ) )
	{
		// Do whatever processing you want
		
		
		cout<< vsAnimal[ vi[0] ] << " " 
			<< vsAnimal[ vi[1] ] << " " 
			<< vsAnimal[ vi[2] ] << endl; 
		
		++Total;
	}

	cout<< "\nTotal : " << Total << 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