Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建具有非常量大小的数组_C++_Arrays_Multidimensional Array_Variable Length Array_Lcs - Fatal编程技术网

C++ 创建具有非常量大小的数组

C++ 创建具有非常量大小的数组,c++,arrays,multidimensional-array,variable-length-array,lcs,C++,Arrays,Multidimensional Array,Variable Length Array,Lcs,我目前正在做一项作业,我必须找到一种方法来输出两个字符串的最长公共子序列。在我发现代码实现的所有其他地方,它们都有一个相似之处:多个数组都是用非常量变量初始化的,我一直认为这是不允许的。当我试图编译这个程序时,由于这个原因我得到了一个错误。这样的代码一开始应该如何编译 #include <iostream> #include <algorithm> #include <vector> using namespace std; //Prints the Lo

我目前正在做一项作业,我必须找到一种方法来输出两个字符串的最长公共子序列。在我发现代码实现的所有其他地方,它们都有一个相似之处:多个数组都是用非常量变量初始化的,我一直认为这是不允许的。当我试图编译这个程序时,由于这个原因我得到了一个错误。这样的代码一开始应该如何编译

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

//Prints the Longest common subsequence
void printLCS(char *s1, char *s2, int m, int n);
/* Driver program to test above function */
int main()
{
char s1[] = "ABCBDAB";
char s2[] = "BDCABA";
printLCS(s1, s2, strlen(s1), strlen(s2));
return 0;
}

void printLCS(char *s1, char *s2, const int m, const int n)
{
int L[m + 1][n + 1];

//Building L[m][n] as in algorithm
for (int i = 0; i <= m; i++)
{
    for (int j = 0; j <= n; j++)
    {
        if (i == 0 || j == 0)
            L[i][j] = 0;
        else if (s1[i - 1] == s2[j - 1])
            L[i][j] = L[i - 1][j - 1] + 1;
        else
            L[i][j] = max(L[i - 1][j], L[i][j - 1]);
    }
}

//To print LCS
int index = L[m][n];
//charcater array to store LCS
char LCS[index + 1];
LCS[index] = '\0'; // Set the terminating character

                   //Stroing characters in LCS
                   //Start from the right bottom corner character
int i = m, j = n;
while (i > 0 && j > 0)
{
    //if current character in s1 and s2 are same, then include this character in LCS[]
    if (s1[i - 1] == s2[j - 1])
    {
        LCS[index - 1] = s1[i - 1]; // Put current character in result
        i--; j--; index--;     // reduce values of i, j and index

    }
    // compare values of L[i-1][j] and L[i][j-1] and go in direction of greater value.
    else if (L[i - 1][j] > L[i][j - 1])
        i--;
    else
        j--;
}

// Print the LCS
cout << "LCS of " << s1 << " and " << s2 << " is " << endl << LCS << endl;
}
#包括
#包括
#包括
使用名称空间std;
//打印最长的公共子序列
无效打印LCS(字符*s1,字符*s2,整数m,整数n);
/*用于测试上述功能的驱动程序*/
int main()
{
字符s1[]=“ABCBDAB”;
字符s2[]=“BDCABA”;
printLCS(s1,s2,strlen(s1),strlen(s2));
返回0;
}
无效打印LCS(字符*s1,字符*s2,常量int m,常量int n)
{
int L[m+1][n+1];
//在算法中构建L[m][n]
对于(int i=0;i 0)
{
//如果s1和s2中的当前字符相同,则将此字符包括在LCS[]
if(s1[i-1]==s2[j-1])
{
LCS[index-1]=s1[i-1];//将当前字符放入结果中
i--;j--;index--;//减少i、j和index的值
}
//比较L[i-1][j]和L[i][j-1]的值,并朝着更大值的方向前进。
else如果(L[i-1][j]>L[i][j-1])
我--;
其他的
j--;
}
//打印LCS

CUT

在GCC编译器中有一个非标准扩展,大多数人使用它来允许可变长度数组。但是,您不应该使用它,因为VLAS有,这就是为什么它们最初不在C++标准中。而且,当程序由于尝试接收到大量输入时,可能会出现堆栈溢出。正在初始化以在堆栈上创建一个大数组

使数组大小恒定或使用
std::vector
添加
\include
(用于
strlen()
) 使我能够编译它: 和产出

LCS of ABCBDAB and BDCABA is 
BDAB
哪个是正确的,不是吗?(我不知道LCS)[


当性能不是绝对优先级时,你可以考虑使用<代码> STD::vector 这有很多障碍(也包括向量类但没有使用它)?

如果您的问题指定了输入大小的最大限制,只需将数组设置为常量大小即可。不过,您必须小心不要在堆栈上创建大数组。您也可以使用
std::vector
,这是最佳实践。这是我开始编写自己的程序时使用的方法,我一直在想如何才能做到这一点上面的代码甚至应该是compile.GCC扩展名-compile with
-pedantic
。可能的重复:“…您可能会以stackoverflow结束…”!是的,他必须以stackoverflow结束以找到进一步的解决方案。