所有排列,并找到最小的数字c++

All permutations, and find the lowest number c++

我正在编写 C++,我想这样做:

我的输入:

213

我想创建所有排列并生成最小的数字。喜欢:

123 -> lowest number
132
231
213
312
321

我可以使用,next_permutation (std library),但是,这是最有效的方法吗?

不像 Kerrek SB 所说的那样,您可以对其进行排序并取用(默认升序)。

std::vector<int> input = {
    4, 3, 9, 1
};
std::sort(std::begin(input), std::end(input));

有关详细信息,请参阅:std::sort。如果你真的需要所有的排列,你可以在排序后的输入上使用 std::next_permutation,它总是第一个。