生成字符串向量的所有组合

Generating all the Combinations of a vector of strings

本文关键字:组合 向量 字符串      更新时间:2023-10-16

我试图解决一些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) );