abcd ef-6ren">
gpt4 book ai didi

c++ - 生成字符串 vector 的所有组合

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:09:08 26 4
gpt4 key购买 nike

我正在尝试解决 UVA 的一些问题,我想生成字符串数组的所有可能组合。例如:

    string str[]={"abcd","efg","hij"};

所以程序必须打印:

    >abcd efg hij
>abcd hij efg
>hij abcd efg
>hij efg abcd
>efg abcd hij
>efg hij abcd

最佳答案

我认为您正在寻找 STL 的 next_permutation算法。

应用于您的示例,它应该看起来像这样:

std::sort (str, str+3);

std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << str[0] << ' ' << str[1] << ' ' << str[2] << '\n';
} while ( std::next_permutation(str, str+3) );

关于c++ - 生成字符串 vector 的所有组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com