2010-09-11 84 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; 
} 

這將通過字符串的所有組合排列替換。