代码之家  ›  专栏  ›  技术社区  ›  Vepir

嵌套for循环结构[复制]

  •  0
  • Vepir  · 技术社区  · 6 年前

    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)
    

    现在我想得到索引的所有组合:

    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
    

    ---------------------代码:这是我使用的一个示例代码。

    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;
    }
    

    它现在不显示所有的组合。。

    如果我让代码只对这个例子起作用(试验尺寸()==5) 我只用了五次循环,比如:

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

    然后给出所有的组合。

    0 回复  |  直到 7 年前
        1
  •  2
  •   Jarod42    7 年前

    您可以:

    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