C++一个向量的所有组合

C++ All combinations of a vector

本文关键字:组合 向量 一个 C++      更新时间:2023-10-16

假设我们有一些来自0 to n的数字,我们希望对大小为s的数字进行加扰,并希望看到所有可能的组合。

因此,排列的数量恰好等于s! * n!/(s!*(n-s)!)

n = 3s = 3的示例:

0 1 2 | 0 1 3 | 0 2 1 | 0 2 3 | 0 3 1 | 0 3 2 | 1 0 2 | 1 0 3 | 1 3 2 
1 2 3 | 1 2 0 | 1 3 0 | 2 0 1 | 2 1 0 | 2 0 3 | 2 3 0 | 2 3 1 | 2 1 3
            3 0 1 | 3 1 0 | 3 0 2 | 3 2 0 | 3 1 2 | 3 2 1

使用boost/stl有没有一种平滑的方法来实现这一点?

以下是使用T.C.在注释中引用的链接的代码(http://howardhinnant.github.io/combinations.html):

#include "../combinations/combinations"
#include <iostream>
#include <vector>
int
main()
{
    std::vector<int> v{0, 1, 2, 3};
    typedef std::vector<int>::iterator Iter;
    for_each_permutation(v.begin(), v.begin()+3, v.end(),
        [](Iter f, Iter l)
        {
            for (; f != l; ++f)
                std::cout << *f << ' ';
            std::cout << "| ";
            return false;
        }
    );
    std::cout << 'n';
}
0 1 2 | 0 2 1 | 1 0 2 | 1 2 0 | 2 0 1 | 2 1 0 | 0 1 3 | 0 3 1 | 1 0 3 | 1 3 0 | 3 0 1 | 3 1 0 | 0 2 3 | 0 3 2 | 2 0 3 | 2 3 0 | 3 0 2 | 3 2 0 | 1 2 3 | 1 3 2 | 2 1 3 | 2 3 1 | 3 1 2 | 3 2 1 | 

std::next_permutation相比,该库的一个显著优点是,被排列的元素不需要排序,甚至不需要小于可比元素。例如:

#include "../combinations/combinations"
#include <iostream>
#include <vector>
enum class color
{
    red,
    green,
    blue,
    cyan
};
std::ostream&
operator<< (std::ostream& os, color c)
{
    switch (c)
    {
    case color::red:
        os << "red";
        break;
    case color::green:
        os << "green";
        break;
    case color::blue:
        os << "blue";
        break;
    case color::cyan:
        os << "cyan";
        break;
    }
    return os;
}
int
main()
{
    std::vector<color> v{color::blue, color::red, color::cyan, color::green};
    typedef std::vector<color>::iterator Iter;
    for_each_permutation(v.begin(), v.begin()+3, v.end(),
        [](Iter f, Iter l)
        {
            for (; f != l; ++f)
                std::cout << *f << ' ';
            std::cout << "| ";
            return false;
        }
    );
    std::cout << 'n';
}

蓝-红-青|蓝-青-红|红-蓝-青|红-青-蓝|青蓝-红|青-红-蓝|蓝-红-绿|蓝-绿-红|红-蓝绿|红-绿-蓝|绿-蓝-红|绿-红-蓝|蓝-青绿色|蓝绿色青色|青蓝色绿色|青绿色蓝色|绿色蓝-青|绿-青-蓝|红-青-绿|红-绿-青|青红-绿|青-绿-红|绿-红-青|绿-青-红|

现场演示

#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
void dfs(int depth, int s, int i, std::vector<int>& c, const std::vector<int>& v)
{
    if (depth == s)
    {
        do
        {
            std::copy(c.begin(), c.end(), std::ostream_iterator<int>(std::cout, " "));
            std::cout << "| ";
        }
        while (std::next_permutation(c.begin(), c.end()));
    }
    else
    {
        for (int j = i + 1; j < (int)v.size(); ++j)
        {
            c.push_back(v[j]);
            dfs(depth + 1, s, j, c, v);
            c.pop_back();
        }
    }
}
int main()
{
    std::vector<int> v{ 0, 1, 2, 3 };
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());    
    std::vector<int> c;
    const int length = 3;
    dfs(0, length, -1, c, v);
}

输出:

0 1 2 | 0 2 1 | 1 0 2 | 1 2 0 | 2 0 1 | 2 1 0 | 0 1 3 | 0 3 1 | 1 0 3 |
1 3 0 | 3 0 1 | 3 1 0 | 0 2 3 | 0 3 2 | 2 0 3 | 2 3 0 | 3 0 2 | 3 2 0 |
1 2 3 | 1 3 2 | 2 1 3 | 2 3 1 | 3 1 2 | 3 2 1