2013-02-25 112 views
0
#include<iostream> 
#include<vector> 
#include<algorithm> 

using namespace std; 
int main() 
{ 
    std::vector<int> v; 
    long int a1[1000000]; 
    string a[1000000]; 
    for (int i=0; i<100; i++) 
     a[i]=" "; 
    int n; 
    cout << "enter the value of n"; 
    cin >> n; 
    for (int i=0; i<n; i++) 
    { 
     cin >> a1[i]; 
     v.push_back(a1[i]); 
    } 
    sort(v.begin(), v.end()); 
    char ch[100]; 
    int i=0; 
    do { 
     for(int j=0; j<n; j++) 
     { 
      ch[j] = v[j] + '0'; 
      // cout<<ch[j]; 
     } 
     int j=3; 
     int k=0; 
     for(int l=0; l<n; l++) 
     { 
      a[i] = a[i] + ch[l]; 
     } 
     cout << a[i] << endl; 
     i++; 
    } 
    while (std::next_permutation(v.begin(), v.end())); 
    cout << endl << i; 
} 

我想將所有排列存儲在字符串數組中,但我無法存儲> 8!(40320)即9!如果我聲明字符串a [1000000]它顯示dev C++錯誤可以任何人解釋我如何將它存儲一個字符串數組speifically(因爲我想這個字符串代碼在另一個代碼,這使得它更容易)大於9 !或高達15!如何在字符串數組中存儲更多數量的字符串

+0

你想在容器中存儲多達15個字符串?你確定你知道它有多大,它需要多少存儲空間? – Blastfurnace 2013-02-25 18:43:25

+0

@Blastfurnace使用容器可能需要大容量內存,我希望達到10^15的存儲容量,因爲我想在做spoj – 2013-02-25 18:58:12

+0

認真地說,拿出一個計算器,估計需要多少內存/地址空間,超過1.3萬億個'std :: string'對象及其內容。如果你這樣做是因爲一個騙局問題,你可能正在嘗試一個荒謬的不切實際的暴力解決方案。 – Blastfurnace 2013-02-25 19:15:49

回答

4

堆棧溢出。

對於這樣多的元素,堆棧太小。您需要使用堆(使用operator new)。或者只是使用std::vector

例如,你可以用

std::vector<std::string> a(1000000); 

同爲long int更換

string a[1000000]; 

+0

我不認爲這堆會足夠15或! – Daniel 2013-02-25 18:53:13

+0

@meh使用容器可能需要大容量內存,我想要高達10^15的存儲是否有任何替代 – 2013-02-25 18:55:54

+0

@ user1670364 - 你應該做一些研究。這有不同的技術。另外,這取決於你想要做什麼。這是一個複雜的任務。 – 2013-02-25 19:33:32

0

寫入到一個文件沖洗它定期。 在內存中緩存所需的少數內存。