gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:18:15 25 4
gpt4 key购买 nike

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com