2010-09-11 47 views
0

的所有排列字符串我有以下代码生成字符

#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; 
} 

我要生成的字符串,但这里是错误

1>------ Build started: Project: string_combinations, Configuration: Debug Win32 ------ 
1>Build started 9/11/2010 12:42:08 PM. 
1>InitializeBuildStatus: 
1> Touching "Debug\string_combinations.unsuccessfulbuild". 
1>ClCompile: 
1> string_combinations.cpp 
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments 
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments 
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments 
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments 
1> 
1>Build FAILED. 
1> 
1>Time Elapsed 00:00:00.82 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

请帮我感到困惑,为什么我不能直接从转换字符串通过此方法字符串(char)

回答

3

问题与此表单的表达式:

(new string *)(c1) 

左手边不是一个类型,它是一个表达式。当您用另一个括号表达式对它进行后缀时,它看起来像一个函数调用,但只有当左表达式是函数名称或函数指针时才起作用。在这种情况下,新表达式的类型为std::string**,它不是函数指针。

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

std::string(1, c1); 

你可以做类似的事情。

return std::string(1, c1) + std::string(1, c2); 

注意,也没有在任何地方打电话生成和如果从for循环,你不打算通过所有的组合迭代的第一次迭代,做return你只会每产生第一compination。

2

您应该使用stringstream创建你的字符串如下:

stringstream s;
s << c1 << c2 << c3 << c4 << ends;
return s.str();

-1

所以,我看你是在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())); 
} 
+0

请注意事实上有24! = 620448401733239439360000可能的排列,所以运行上述程序(存储每个排列的版本)将会以'内存不足'异常爆炸。运行在自己的risc上,而不是在一台控制着核反应器或类似的东西的计算机上运行! – 2010-09-11 11:09:07

+1

原来的代码(我解释它)是写所有4个字符的字符串 - 只有26^4的可能性。 – DanJ 2010-09-11 12:18:30

+0

-1:这是您在代码中使用的所有内容的滥用。 – rubenvb 2010-09-11 12:33:43

5

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

下面是一些简单的代码:

#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; 
} 

这将通过字符串的所有组合排列替换。