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.
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include"combination.h"

using namespace std;
using namespace stdcomb;


set<string> s1;
pair< set<string>::iterator, bool > pr;
unsigned int errors=0;

void processit(char* a)
{
  
  string str(a);
  try
  {
  pr=s1.insert(str);
  if (pr.second==false)
    ++errors;
  }
  catch ( exception &e ) 
  {
      cerr << "An error has occurred" << endl;
      cerr << "Caught: " << e.what( ) << endl;
      cerr << "Type: " << typeid( e ).name( ) << endl;
  };

}

int main()
{

  char n[]="123456";
  char r[]="1234";
  char r1[]="1234";
  unsigned int count=0;

  do
  {
    ++count;
    strncpy(r1,r,4);
    processit(r);
    while(next_permutation(r1,r1+4))
    {
      ++count;
      processit(r1);
    }
  }
  while(next_combination(n,n+6,r,r+4));

  
  //Printing
  //for(set<string>::const_iterator it=s1.begin();it!=s1.end();++it)
  //      cout<<*it<<endl;
  
  
  cout<<"errors = "<<errors<<endl;
  cout<<"Total number of unique permutations : "<<count-errors<<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 | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 13 Sep 2009
Article Copyright 2003 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid