dimanche 3 novembre 2019

How can I make the cartesian product fo the members of a string or vector given a length?

I'm trying to make a program that gives me all the possible combinations of a vector or string members. I saw that it can be done with the std::cartesian_product function in C++, but all the examples I saw were between two lists. What I need is as follows:

If a have a vector like:

std::vector<std::string> letters = {"a", "b", "c", ..., "z"};

I want all the combiantions with different lengths. For example, if I want all the combiantions of minimum length 1 and maximum 2, it should be like:

a, b, c, d, ... , z, ab, ac, ad, ... , az, ... , zz

In Python, I did it using the itertools library, with the product function. I did it like this:

for i in range(1, 3):
    for s in intertools.product(vector, repeat=i):
        print(''.join(s))

How should I do it? I'm a little bit lost with this.

Thanks!!

Aucun commentaire:

Enregistrer un commentaire