Click here to Skip to main content
13,295,192 members (57,772 online)
Click here to Skip to main content

Stats

253.1K views
5.4K downloads
70 bookmarked
Posted 12 Apr 2016

Combinations in C++

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

Cake Processor
Software Developer (Senior)
United States United States

C++ developer transitioning to Python


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
Web02 | 2.8.171207.1 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid