如何在字符串数组中存储更多数量的字符串

how to store more numbers of strings in an string array

本文关键字:字符串 数组 存储      更新时间:2023-10-16
#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++中显示错误,任何人都可以解释我如何将它存储为大于9的字符串数组(因为我希望将此字符串代码存储在另一个代码中,这会更容易(吗!或高达15!

堆栈溢出。

堆栈太小,无法容纳这样数量的元素。您需要使用堆(使用operator new(。或者只使用std::vector

例如,您可以更换

string a[1000000];

带有

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

long int相同。

写入一个定期刷新的文件。在内存中缓存所需的几个。