C++ 生成具有所有字符排列的字符串

C++ 生成具有所有字符排列的字符串,c++,C++,我有以下代码 #include <iostream> #include <string> using namespace std; string generate(){ for (char c1='A';c1<='Z';c1++){ for (char c2='A';c2 <='Z';c2++){ for (char c3='A';c3<='Z';c3++){

我有以下代码

#include <iostream>
#include <string>
using namespace std;
string generate(){
     for (char c1='A';c1<='Z';c1++){
          for (char c2='A';c2 <='Z';c2++){
               for (char c3='A';c3<='Z';c3++){
                    for (char c4='A';c4<='Z';c4++){


                         return  (new string *)(c1) + (new string*)(c2)+(new string*)(c3)+(new string*)(c4);
                    }
               }
          }
     }


}
int main(){




     return 0;
}

请帮助我,我很困惑为什么我不能用stringchar方法直接从char转换为string。问题在于您的这种形式的表达式:

(new string *)(c1)
左手边不是类型,而是表达式。当使用另一个括号表达式作为后缀时,它看起来像是函数调用,但仅当左表达式是函数名或函数指针时,它才起作用。在这种情况下,新表达式的类型std::string**不是函数指针

要从单个字符构造临时字符串,不应使用动态分配对象的new;相反,您可以使用构造函数。一个合适的方法是使用一个计数和一个字符来重复该计数。在您的情况下,计数1是您想要的:

std::string(1, c1);
你可以这样做

return std::string(1, c1) + std::string(1, c2);
请注意,您也不会在任何地方调用generate,如果您从for循环的第一次迭代返回,您将不会对所有组合进行迭代,您将只生成第一次编译。

您应该使用a来创建字符串,如下所示:

细绳;
所以,我看到你在VC++10上:

#include <array>
#include <algorithm>
#include <list>
#include <string>

int main() {

    std::array<char, 24> tAlphabet = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // and so on...
    };

    // you could store permutations here, but there will be really, really many of them
    std::list<std::string> tAllPermutations;
    do {
        std::string tCurrentPermutation;

        std::for_each(tAlphabet.begin(), tAlphabet.end(),
            [&tCurrentPermutation] (char tCurrentChar) -> void {
                tCurrentPermutation += tCurrentChar;
        });

        std::cout << tCurrentPermutation << std::endl;
    } while (std::next_permutation(tAlphabet.begin(), tAlphabet.end()));
}

我很肯定,手动循环可以避免使用。像这样的手动循环非常糟糕,特别是当标准库预见到这种情况时

下面是一些快速代码:

#include <algorithm>
    using std::next_permutation;
#include <iostream>
    using std::cout;
    using std::endl;
#include <string>
    using std::string;

int main()
{
    string currentPermutation = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    size_t i=0;
    do
    {
        cout << "permutation " << i << ": " << currentPermutation << endl;
        ++i;
    } while( next_permutation(currentPermutation.begin(), currentPermutation.end()) );
    return 0;
}

这将在字符串的所有组合中进行置换。

请注意有24个!=620448401733239439360000个可能的排列,因此运行上述程序时,存储每个排列的版本将爆发“内存不足”异常。在自己的risc上运行,而不是在控制核电站或类似设备的计算机上运行。。。!我解释的原始代码是编写所有4个字符串-只有26^4种可能性。-1:这是对代码中使用的所有内容的滥用。@rubenvb误用?你必须解释一下@丹吉:哦,没关系,那么现在才4点!可能性。我实际上误读了OP的意图,现在看到所有额外的代码都很好地满足了它的目的。对不起,我的错。在编辑之前无法撤消我的否决票:
#include <algorithm>
    using std::next_permutation;
#include <iostream>
    using std::cout;
    using std::endl;
#include <string>
    using std::string;

int main()
{
    string currentPermutation = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    size_t i=0;
    do
    {
        cout << "permutation " << i << ": " << currentPermutation << endl;
        ++i;
    } while( next_permutation(currentPermutation.begin(), currentPermutation.end()) );
    return 0;
}