B
Brad
Here is some code I wrote that breaks up a Cartesian Product. It
compiles and runs fine:
#include <iostream>
#include <cmath>
void cartesian_product( const std::string& my_string, int b1, int e1 )
{
std::string::const_iterator it1, it2, it3;
std::string possibility;
possibility.reserve( 3 );
for ( it1 = my_string.begin()+b1; it1 != my_string.end()-e1; ++it1 )
{
possibility.push_back( *it1 );
for ( it2 = my_string.begin(); it2 != my_string.end(); ++it2 )
{
possibility.push_back( *it2 );
for ( it3 = my_string.begin(); it3 != my_string.end(); ++it3 )
{
possibility.push_back( *it3 );
std::cout << possibility << std::endl;
possibility.resize( 2 );
}
possibility.resize( 1 );
}
}
possibility.clear();
//// Show the break-ups
std::cout << "---" << std::endl;
}
int main ()
{
const std::string my_string ("abc"); // The string you wish to
generate the CP from.
int magic = my_string.size()-1; // Always 1 less than your string
size.
int j = 0;
// The number of character... must match number of loops in
cartesian_product()
int length = 3;
double total = pow( my_string.size(), length );
std::clog << "Total Combinations = " << total << std::endl;
// Generate CP chunks
for ( int i = magic; i >= 0; --i )
{
cartesian_product( my_string, j, i );
++j;
}
}
------------------------------
You can produce one full (unchunked) CP by doing this:
cartesian_product( my_string, 0, 0 );
I'd like to make it so that I pass an int (say 2, 3 or 4) to the
cartesian_product function so that it would produce 2, 3 or 4 chunks.
Basically control the number of chunks it produces. So for example if
there are 64 possibilities and I pass in 2, then the first run
produces the first 32 possibilities and the second the last 32. Any
suggestions?
Thanks,
Brad
compiles and runs fine:
#include <iostream>
#include <cmath>
void cartesian_product( const std::string& my_string, int b1, int e1 )
{
std::string::const_iterator it1, it2, it3;
std::string possibility;
possibility.reserve( 3 );
for ( it1 = my_string.begin()+b1; it1 != my_string.end()-e1; ++it1 )
{
possibility.push_back( *it1 );
for ( it2 = my_string.begin(); it2 != my_string.end(); ++it2 )
{
possibility.push_back( *it2 );
for ( it3 = my_string.begin(); it3 != my_string.end(); ++it3 )
{
possibility.push_back( *it3 );
std::cout << possibility << std::endl;
possibility.resize( 2 );
}
possibility.resize( 1 );
}
}
possibility.clear();
//// Show the break-ups
std::cout << "---" << std::endl;
}
int main ()
{
const std::string my_string ("abc"); // The string you wish to
generate the CP from.
int magic = my_string.size()-1; // Always 1 less than your string
size.
int j = 0;
// The number of character... must match number of loops in
cartesian_product()
int length = 3;
double total = pow( my_string.size(), length );
std::clog << "Total Combinations = " << total << std::endl;
// Generate CP chunks
for ( int i = magic; i >= 0; --i )
{
cartesian_product( my_string, j, i );
++j;
}
}
------------------------------
You can produce one full (unchunked) CP by doing this:
cartesian_product( my_string, 0, 0 );
I'd like to make it so that I pass an int (say 2, 3 or 4) to the
cartesian_product function so that it would produce 2, 3 or 4 chunks.
Basically control the number of chunks it produces. So for example if
there are 64 possibilities and I pass in 2, then the first run
produces the first 32 possibilities and the second the last 32. Any
suggestions?
Thanks,
Brad