print out all combinations of index

Multi tool use
Multi tool use












0















I set a vector list, for example :



vector<VectorXi> Test;
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2));
Test.push_back(VectorXi(0));
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2,3));
PrintAllCombins(Test)


And now I want to get all combinations of indexes :



0, 0, 0, 0, 0
0, 0, 0, 0, 1
0, 0, 0, 0, 2
0, 0, 0, 0, 3

0, 0, 0, 1, 0
0, 0, 0, 1, 1
0, 0, 0, 1, 2
0, 0, 0, 1, 3

0, 0, 1, 0, 0
0, 0, 1, 0, 1
0, 0, 1, 0, 2
0, 0, 1, 0, 3

... and so on


If i use for or while loop suitably, then it works I guess, but I encounter limitation. Is there any idea? I'm writing code in c and c++



--------------------- code : this is an example code that im using.



vector<VectorXi> Test;
VectorXi a0(2); a0[0] = 0; a0[1] = 1;
VectorXi a1(3); a1[0] = 0; a1[1] = 1; a1[2] = 2;
VectorXi a2(2); a2[0] = 0; a2[1] = 1;
VectorXi a3(4); a3[0] = 0; a3[1] = 1; a3[2] = 2; a3[3] = 3;
VectorXi a5(1); a5[0] = 0;
Test.push_back(a0);
Test.push_back(a1);
Test.push_back(a5);
Test.push_back(a2);
Test.push_back(a3);

VectorXi index(5);
for (int i = 0; i < 5; i++)
index[i] = 0;

int IndexTemp = Test.size()-1;
vector<VectorXi> result;
bool c = true;
while (c == true)
{
if (index[IndexTemp] < Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
index[IndexTemp] ++;
}
else if (index[IndexTemp] == Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
IndexTemp--;
if (IndexTemp < 0)
break;
index[IndexTemp] ++;
}

}


for (unsigned int i = 0; i < result.size(); i++)
{
cout << i << " : ";
for (unsigned int j = 0; j < result[i].size(); j++)
{
cout << result[i](j) << " ";
}
cout << endl;
}


It does not show all combinations now..



If I make code to work only to this example (Test.size() == 5)
I just use for loop five times like :



for(Test[0].size())
for(Test[1].size())
for(Test[2].size())
for(Test[3].size())
for(Test[4].size())
cout << ~~~~~


Then it gives all combinations.
However if the Test.size() increased, I cannot write all for loops manually.










share|improve this question

























  • What have you tried doing? show the code. Also explain what limitations you encountered

    – UnholySheep
    Mar 30 '17 at 7:48











  • What is VectorXi?

    – Shridhar R Kulkarni
    Mar 30 '17 at 7:52











  • Similar to how-to-get-cartesian-product-from-different-group-member and algorithm-to-get-cartesian-product

    – Jarod42
    Mar 30 '17 at 8:02











  • Thanks! it is the solution I am looking for!

    – Wooni
    Mar 30 '17 at 8:06
















0















I set a vector list, for example :



vector<VectorXi> Test;
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2));
Test.push_back(VectorXi(0));
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2,3));
PrintAllCombins(Test)


And now I want to get all combinations of indexes :



0, 0, 0, 0, 0
0, 0, 0, 0, 1
0, 0, 0, 0, 2
0, 0, 0, 0, 3

0, 0, 0, 1, 0
0, 0, 0, 1, 1
0, 0, 0, 1, 2
0, 0, 0, 1, 3

0, 0, 1, 0, 0
0, 0, 1, 0, 1
0, 0, 1, 0, 2
0, 0, 1, 0, 3

... and so on


If i use for or while loop suitably, then it works I guess, but I encounter limitation. Is there any idea? I'm writing code in c and c++



--------------------- code : this is an example code that im using.



vector<VectorXi> Test;
VectorXi a0(2); a0[0] = 0; a0[1] = 1;
VectorXi a1(3); a1[0] = 0; a1[1] = 1; a1[2] = 2;
VectorXi a2(2); a2[0] = 0; a2[1] = 1;
VectorXi a3(4); a3[0] = 0; a3[1] = 1; a3[2] = 2; a3[3] = 3;
VectorXi a5(1); a5[0] = 0;
Test.push_back(a0);
Test.push_back(a1);
Test.push_back(a5);
Test.push_back(a2);
Test.push_back(a3);

VectorXi index(5);
for (int i = 0; i < 5; i++)
index[i] = 0;

int IndexTemp = Test.size()-1;
vector<VectorXi> result;
bool c = true;
while (c == true)
{
if (index[IndexTemp] < Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
index[IndexTemp] ++;
}
else if (index[IndexTemp] == Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
IndexTemp--;
if (IndexTemp < 0)
break;
index[IndexTemp] ++;
}

}


for (unsigned int i = 0; i < result.size(); i++)
{
cout << i << " : ";
for (unsigned int j = 0; j < result[i].size(); j++)
{
cout << result[i](j) << " ";
}
cout << endl;
}


It does not show all combinations now..



If I make code to work only to this example (Test.size() == 5)
I just use for loop five times like :



for(Test[0].size())
for(Test[1].size())
for(Test[2].size())
for(Test[3].size())
for(Test[4].size())
cout << ~~~~~


Then it gives all combinations.
However if the Test.size() increased, I cannot write all for loops manually.










share|improve this question

























  • What have you tried doing? show the code. Also explain what limitations you encountered

    – UnholySheep
    Mar 30 '17 at 7:48











  • What is VectorXi?

    – Shridhar R Kulkarni
    Mar 30 '17 at 7:52











  • Similar to how-to-get-cartesian-product-from-different-group-member and algorithm-to-get-cartesian-product

    – Jarod42
    Mar 30 '17 at 8:02











  • Thanks! it is the solution I am looking for!

    – Wooni
    Mar 30 '17 at 8:06














0












0








0








I set a vector list, for example :



vector<VectorXi> Test;
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2));
Test.push_back(VectorXi(0));
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2,3));
PrintAllCombins(Test)


And now I want to get all combinations of indexes :



0, 0, 0, 0, 0
0, 0, 0, 0, 1
0, 0, 0, 0, 2
0, 0, 0, 0, 3

0, 0, 0, 1, 0
0, 0, 0, 1, 1
0, 0, 0, 1, 2
0, 0, 0, 1, 3

0, 0, 1, 0, 0
0, 0, 1, 0, 1
0, 0, 1, 0, 2
0, 0, 1, 0, 3

... and so on


If i use for or while loop suitably, then it works I guess, but I encounter limitation. Is there any idea? I'm writing code in c and c++



--------------------- code : this is an example code that im using.



vector<VectorXi> Test;
VectorXi a0(2); a0[0] = 0; a0[1] = 1;
VectorXi a1(3); a1[0] = 0; a1[1] = 1; a1[2] = 2;
VectorXi a2(2); a2[0] = 0; a2[1] = 1;
VectorXi a3(4); a3[0] = 0; a3[1] = 1; a3[2] = 2; a3[3] = 3;
VectorXi a5(1); a5[0] = 0;
Test.push_back(a0);
Test.push_back(a1);
Test.push_back(a5);
Test.push_back(a2);
Test.push_back(a3);

VectorXi index(5);
for (int i = 0; i < 5; i++)
index[i] = 0;

int IndexTemp = Test.size()-1;
vector<VectorXi> result;
bool c = true;
while (c == true)
{
if (index[IndexTemp] < Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
index[IndexTemp] ++;
}
else if (index[IndexTemp] == Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
IndexTemp--;
if (IndexTemp < 0)
break;
index[IndexTemp] ++;
}

}


for (unsigned int i = 0; i < result.size(); i++)
{
cout << i << " : ";
for (unsigned int j = 0; j < result[i].size(); j++)
{
cout << result[i](j) << " ";
}
cout << endl;
}


It does not show all combinations now..



If I make code to work only to this example (Test.size() == 5)
I just use for loop five times like :



for(Test[0].size())
for(Test[1].size())
for(Test[2].size())
for(Test[3].size())
for(Test[4].size())
cout << ~~~~~


Then it gives all combinations.
However if the Test.size() increased, I cannot write all for loops manually.










share|improve this question
















I set a vector list, for example :



vector<VectorXi> Test;
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2));
Test.push_back(VectorXi(0));
Test.push_back(VectorXi(0,1));
Test.push_back(VectorXi(0,1,2,3));
PrintAllCombins(Test)


And now I want to get all combinations of indexes :



0, 0, 0, 0, 0
0, 0, 0, 0, 1
0, 0, 0, 0, 2
0, 0, 0, 0, 3

0, 0, 0, 1, 0
0, 0, 0, 1, 1
0, 0, 0, 1, 2
0, 0, 0, 1, 3

0, 0, 1, 0, 0
0, 0, 1, 0, 1
0, 0, 1, 0, 2
0, 0, 1, 0, 3

... and so on


If i use for or while loop suitably, then it works I guess, but I encounter limitation. Is there any idea? I'm writing code in c and c++



--------------------- code : this is an example code that im using.



vector<VectorXi> Test;
VectorXi a0(2); a0[0] = 0; a0[1] = 1;
VectorXi a1(3); a1[0] = 0; a1[1] = 1; a1[2] = 2;
VectorXi a2(2); a2[0] = 0; a2[1] = 1;
VectorXi a3(4); a3[0] = 0; a3[1] = 1; a3[2] = 2; a3[3] = 3;
VectorXi a5(1); a5[0] = 0;
Test.push_back(a0);
Test.push_back(a1);
Test.push_back(a5);
Test.push_back(a2);
Test.push_back(a3);

VectorXi index(5);
for (int i = 0; i < 5; i++)
index[i] = 0;

int IndexTemp = Test.size()-1;
vector<VectorXi> result;
bool c = true;
while (c == true)
{
if (index[IndexTemp] < Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
index[IndexTemp] ++;
}
else if (index[IndexTemp] == Test[IndexTemp].size()-1)
{
VectorXi T;
T.resize(Test.size());
for (int j = 0; j<Test.size(); j++)
{
T[j] = Test[j](index[j]);
}
result.push_back(T);
IndexTemp--;
if (IndexTemp < 0)
break;
index[IndexTemp] ++;
}

}


for (unsigned int i = 0; i < result.size(); i++)
{
cout << i << " : ";
for (unsigned int j = 0; j < result[i].size(); j++)
{
cout << result[i](j) << " ";
}
cout << endl;
}


It does not show all combinations now..



If I make code to work only to this example (Test.size() == 5)
I just use for loop five times like :



for(Test[0].size())
for(Test[1].size())
for(Test[2].size())
for(Test[3].size())
for(Test[4].size())
cout << ~~~~~


Then it gives all combinations.
However if the Test.size() increased, I cannot write all for loops manually.







c++ cartesian-product






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 30 '17 at 8:02









Jarod42

114k12101182




114k12101182










asked Mar 30 '17 at 7:46









WooniWooni

93110




93110













  • What have you tried doing? show the code. Also explain what limitations you encountered

    – UnholySheep
    Mar 30 '17 at 7:48











  • What is VectorXi?

    – Shridhar R Kulkarni
    Mar 30 '17 at 7:52











  • Similar to how-to-get-cartesian-product-from-different-group-member and algorithm-to-get-cartesian-product

    – Jarod42
    Mar 30 '17 at 8:02











  • Thanks! it is the solution I am looking for!

    – Wooni
    Mar 30 '17 at 8:06



















  • What have you tried doing? show the code. Also explain what limitations you encountered

    – UnholySheep
    Mar 30 '17 at 7:48











  • What is VectorXi?

    – Shridhar R Kulkarni
    Mar 30 '17 at 7:52











  • Similar to how-to-get-cartesian-product-from-different-group-member and algorithm-to-get-cartesian-product

    – Jarod42
    Mar 30 '17 at 8:02











  • Thanks! it is the solution I am looking for!

    – Wooni
    Mar 30 '17 at 8:06

















What have you tried doing? show the code. Also explain what limitations you encountered

– UnholySheep
Mar 30 '17 at 7:48





What have you tried doing? show the code. Also explain what limitations you encountered

– UnholySheep
Mar 30 '17 at 7:48













What is VectorXi?

– Shridhar R Kulkarni
Mar 30 '17 at 7:52





What is VectorXi?

– Shridhar R Kulkarni
Mar 30 '17 at 7:52













Similar to how-to-get-cartesian-product-from-different-group-member and algorithm-to-get-cartesian-product

– Jarod42
Mar 30 '17 at 8:02





Similar to how-to-get-cartesian-product-from-different-group-member and algorithm-to-get-cartesian-product

– Jarod42
Mar 30 '17 at 8:02













Thanks! it is the solution I am looking for!

– Wooni
Mar 30 '17 at 8:06





Thanks! it is the solution I am looking for!

– Wooni
Mar 30 '17 at 8:06












1 Answer
1






active

oldest

votes


















1














You may do:



bool increase(const std::vector<std::vector<int>>& v, std::vector<std::size_t>& it)
{
for (std::size_t i = 0, size = it.size(); i != size; ++i) {
const std::size_t index = size - 1 - i;
++it[index];
if (it[index] >= v[index].size()) {
it[index] = 0;
} else {
return true;
}
}
return false;
}

void do_job(const std::vector<std::vector<int>>& v,
const std::vector<std::size_t>& it)
{
for (std::size_t i = 0; i != it.size(); ++i) {
// TODO: manage case where v[i] is empty if relevant.
std::cout << v[i][it[i]] << " ";
}
std::cout << std::endl;
}

void iterate(const std::vector<std::vector<int>>& v)
{
std::vector<std::size_t> it(v.size(), 0u);

do {
do_job(v, it);
} while (increase(v, it));
}


Live Demo






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43111572%2fprint-out-all-combinations-of-index%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You may do:



    bool increase(const std::vector<std::vector<int>>& v, std::vector<std::size_t>& it)
    {
    for (std::size_t i = 0, size = it.size(); i != size; ++i) {
    const std::size_t index = size - 1 - i;
    ++it[index];
    if (it[index] >= v[index].size()) {
    it[index] = 0;
    } else {
    return true;
    }
    }
    return false;
    }

    void do_job(const std::vector<std::vector<int>>& v,
    const std::vector<std::size_t>& it)
    {
    for (std::size_t i = 0; i != it.size(); ++i) {
    // TODO: manage case where v[i] is empty if relevant.
    std::cout << v[i][it[i]] << " ";
    }
    std::cout << std::endl;
    }

    void iterate(const std::vector<std::vector<int>>& v)
    {
    std::vector<std::size_t> it(v.size(), 0u);

    do {
    do_job(v, it);
    } while (increase(v, it));
    }


    Live Demo






    share|improve this answer




























      1














      You may do:



      bool increase(const std::vector<std::vector<int>>& v, std::vector<std::size_t>& it)
      {
      for (std::size_t i = 0, size = it.size(); i != size; ++i) {
      const std::size_t index = size - 1 - i;
      ++it[index];
      if (it[index] >= v[index].size()) {
      it[index] = 0;
      } else {
      return true;
      }
      }
      return false;
      }

      void do_job(const std::vector<std::vector<int>>& v,
      const std::vector<std::size_t>& it)
      {
      for (std::size_t i = 0; i != it.size(); ++i) {
      // TODO: manage case where v[i] is empty if relevant.
      std::cout << v[i][it[i]] << " ";
      }
      std::cout << std::endl;
      }

      void iterate(const std::vector<std::vector<int>>& v)
      {
      std::vector<std::size_t> it(v.size(), 0u);

      do {
      do_job(v, it);
      } while (increase(v, it));
      }


      Live Demo






      share|improve this answer


























        1












        1








        1







        You may do:



        bool increase(const std::vector<std::vector<int>>& v, std::vector<std::size_t>& it)
        {
        for (std::size_t i = 0, size = it.size(); i != size; ++i) {
        const std::size_t index = size - 1 - i;
        ++it[index];
        if (it[index] >= v[index].size()) {
        it[index] = 0;
        } else {
        return true;
        }
        }
        return false;
        }

        void do_job(const std::vector<std::vector<int>>& v,
        const std::vector<std::size_t>& it)
        {
        for (std::size_t i = 0; i != it.size(); ++i) {
        // TODO: manage case where v[i] is empty if relevant.
        std::cout << v[i][it[i]] << " ";
        }
        std::cout << std::endl;
        }

        void iterate(const std::vector<std::vector<int>>& v)
        {
        std::vector<std::size_t> it(v.size(), 0u);

        do {
        do_job(v, it);
        } while (increase(v, it));
        }


        Live Demo






        share|improve this answer













        You may do:



        bool increase(const std::vector<std::vector<int>>& v, std::vector<std::size_t>& it)
        {
        for (std::size_t i = 0, size = it.size(); i != size; ++i) {
        const std::size_t index = size - 1 - i;
        ++it[index];
        if (it[index] >= v[index].size()) {
        it[index] = 0;
        } else {
        return true;
        }
        }
        return false;
        }

        void do_job(const std::vector<std::vector<int>>& v,
        const std::vector<std::size_t>& it)
        {
        for (std::size_t i = 0; i != it.size(); ++i) {
        // TODO: manage case where v[i] is empty if relevant.
        std::cout << v[i][it[i]] << " ";
        }
        std::cout << std::endl;
        }

        void iterate(const std::vector<std::vector<int>>& v)
        {
        std::vector<std::size_t> it(v.size(), 0u);

        do {
        do_job(v, it);
        } while (increase(v, it));
        }


        Live Demo







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 30 '17 at 8:07









        Jarod42Jarod42

        114k12101182




        114k12101182






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43111572%2fprint-out-all-combinations-of-index%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            bdy5vrWxnEHY7smGNslQzdC5m69,IaMLHUeH D0TCuHHc2r5Ef
            AXBBhwA MhR,3U6YCWkQl2,rIuSDZuwMcBnZQae51 BQoK nQmnuKynUF,H6

            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas