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

标签 c++ string vector

#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!之后,如果我声明 string a[1000000] 它在 dev c++ 中显示错误,谁能解释我如何将它存储在一个字符串数组中(因为我想在另一个代码中使用这个字符串代码,这使得它更容易)大于9!或最多 15 个!

最佳答案

堆栈溢出。

堆栈对于如此多的元素来说太小了。您需要使用堆(使用 operator new)。或者只使用 std::vector

例如,你可以替换

string a[1000000];

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

long int 也是如此。

关于c++ - 如何在字符串数组中存储更多数量的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073550/

相关文章:

c - 在 C 中如何创建动态分配的字符串?

python - 在 python 中重新创建一个类似 shell 的解释器?

haskell - 字节串到向量的转换

python - 二体轨道建模问题

c++ - OpenGL:在 glEnableVertexAttribArray 之后的 INVALID_OPERATION

C++编译错误

c++ - 如何调试 C++ 段错误?

c++ - 如何合并 qtable 行的两个单元格?

java - 什么是缓存字符串?

c++ - 在具有多个要求的 vector 中查找对象