Boost logo

Boost Users :

Subject: Re: [Boost-users] generate structs with all combinations of members from a given struct
From: Hicham Mouline (hicham_at_[hidden])
Date: 2010-02-01 04:08:07


  ----- Original Message -----
  From: Hicham Mouline
  To: boost-users_at_[hidden]
  Sent: Monday, February 01, 2010 12:09 PM
  Subject: Re: [Boost-users] generate structs with all combinations of members from a given struct

My previous runtime solution was wrong.
I just fixed it now.

#include <iostream>
#include <list>

typedef std::list< std::list<size_t> > main_list_t;

void combinations(main_list_t& m, const size_t* arr, size_t arrsize)
{
  if (arrsize==0)
    return;

  const std::list<size_t> empty_list;
  std::list<size_t> l( m.empty()? empty_list: m.back() );
  for (size_t s=0; s<arrsize; ++s) {
    l.push_back( arr[s] );
    m.push_back(l);
    l.pop_back();
    combinations(m, arr+s+1, arrsize-s-1);
  }
}

int main()
{
  const size_t arr[] = { 1, 2, 3, 4, 5 };
  const size_t arrsize = sizeof(arr)/sizeof(size_t);

  main_list_t mm;
  combinations(mm, arr, arrsize);

  std::cout<< "size="<< mm.size() <<std::endl;

  for (main_list_t::const_iterator i = mm.begin(); i!=mm.end(); ++i) {
    const std::list<size_t>& l = *i;
    for (std::list<size_t>::const_iterator ii = l.begin(); ii!=l.end(); ++ii) {
      std::cout<< *ii << '\t';
    }
    std::cout<<std::endl;
  }
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net