Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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_Testing_Dynamic - Fatal编程技术网

C++ 此代码显示错误“;斯图未申报;??我该怎么办

C++ 此代码显示错误“;斯图未申报;??我该怎么办,c++,arrays,testing,dynamic,C++,Arrays,Testing,Dynamic,我知道这个错误是因为我在for循环范围内声明了stu,但这是程序的必要性。我想为每个测试用例声明一个数组(测试用例应该一次全部输入)。建议我一种实现方法。动态内存是一种替代方法吗 #include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { int t; cin>>t; int n[t],g[t];

我知道这个错误是因为我在for循环范围内声明了stu,但这是程序的必要性。我想为每个测试用例声明一个数组(测试用例应该一次全部输入)。建议我一种实现方法。动态内存是一种替代方法吗

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int n[t],g[t];
    int m =0;
    for(int w=0;w<t;t++)
    {
        cin>>n[w]>>g[w];
        int stu[n[w]];
        for(int i=0;i<n[w];i++)
        {
            cin>>stu[i];
        }

    }    
    while(m<t)
    {
        int a,b;    
        int e;
        e = (n[m]*(n[m]-1))/2;
        int diff[e];
        if (g[m]=1)
        {
            cout<<0<<endl;
            return 0;
        }
        b=*(min_element(stu,stu+n[m]-1));
        a=*(max_element(stu,stu+n[m]-1));
        if (g[m]=n[m])
        {
            cout<<a-b<<endl;
            return 0;
        }
        int z = 0;
        for(int j=0;j<(n[m]-1);j++)
        {
            for(int k=(j+1);k<n[m];k++)
            {
                diff[z]=abs(stu[j]-stu[k]);
                ++z;
            }
        }        
        cout<<*(min_element(diff,diff+e-1))<<endl;
        ++m;
    }    
    cin.ignore();
    cin.get();
    return 0;
} 
#包括
#包括
#包括
使用名称空间std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m=0;
对于(intw=0;w>n[w]>>g[w];
int stu[n[w]];
对于(int i=0;i>stu[i];
}
}    

而(m您是在
for
循环的
内部声明
stu
,因此它被限制在循环的范围内。然后您尝试在循环之外使用它,因为它未声明

for(int w=0;w<t;t++)
{
  ...
  int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
  // OK to use stu here
  ...
}    
// stu doesn't exist here
您可以将这些数组替换为
std::vector

#包括
#包括
int main()
{
int t=0;
cin>>t;
std::向量n(t);
std::向量g(t);
std::向量stu。。。;
}

您在
for
循环的
内部声明
stu
,因此它被限制在循环的范围内。然后您尝试在循环外部使用它,而该循环未声明

for(int w=0;w<t;t++)
{
  ...
  int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
  // OK to use stu here
  ...
}    
// stu doesn't exist here
您可以将这些数组替换为
std::vector

#包括
#包括
int main()
{
int t=0;
cin>>t;
std::向量n(t);
std::向量g(t);
std::向量stu。。。;
}
线路

int stu[n[w]];
在一个块内,而在该块外它将不会被看到。您应该将其移出块,但这样做当然不能使用
n[w]
,因为w是循环变量。您可以对
n[w]
可以具有的最大值设置限制,例如

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
  int t;
  cin>>t;
  int n[t],g[t]; // <- supported by some compiler, but not std
  int m =0;
  int stu[MAXV];
  for(int w=0;w<t;t++) {
      cin>>n[w]>>g[w];
      for(int i=0;i<n[w] && i < MAXV;i++) {
        cin>>stu[i];
      }
  }    
  while(m<t) {
      int a,b;    
      int e;
      e = (n[m]*(n[m]-1))/2;
      int diff[e];
      if (g[m]==1) {
        cout<<0<<endl;
        return 0;
      } 
      b=*(min_element(stu,stu+n[m]-1));
      a=*(max_element(stu,stu+n[m]-1));
      if (g[m]==n[m]) {
        cout<<a-b<<endl;
        return 0;
      }
      int z = 0;
      for(int j=0;j<(n[m]-1);j++) {
        for(int k=(j+1);k<n[m];k++) {
          diff[z]=abs(stu[j]-stu[k]);
          ++z;
        }
      }        
      cout<<*(min_element(diff,diff+e-1))<<endl;
      ++m;
  }    
  cin.ignore();
  cin.get();
  return 0;
}        
#包括
#包括
#包括
使用名称空间std;
常数int MAXV=100;
int main()
{
int t;
cin>>t;
int n[t],g[t];//n[w]>>g[w];
对于(int i=0;i>stu[i];
}
}    
而(m

int stu[n[w]];
在一个块内,而在该块外它将不会被看到。您应该将其移出块,但这样做当然不能使用
n[w]
,因为w是循环变量。您可以对
n[w]
可以具有的最大值设置限制,例如

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
  int t;
  cin>>t;
  int n[t],g[t]; // <- supported by some compiler, but not std
  int m =0;
  int stu[MAXV];
  for(int w=0;w<t;t++) {
      cin>>n[w]>>g[w];
      for(int i=0;i<n[w] && i < MAXV;i++) {
        cin>>stu[i];
      }
  }    
  while(m<t) {
      int a,b;    
      int e;
      e = (n[m]*(n[m]-1))/2;
      int diff[e];
      if (g[m]==1) {
        cout<<0<<endl;
        return 0;
      } 
      b=*(min_element(stu,stu+n[m]-1));
      a=*(max_element(stu,stu+n[m]-1));
      if (g[m]==n[m]) {
        cout<<a-b<<endl;
        return 0;
      }
      int z = 0;
      for(int j=0;j<(n[m]-1);j++) {
        for(int k=(j+1);k<n[m];k++) {
          diff[z]=abs(stu[j]-stu[k]);
          ++z;
        }
      }        
      cout<<*(min_element(diff,diff+e-1))<<endl;
      ++m;
  }    
  cin.ignore();
  cin.get();
  return 0;
}        
#包括
#包括
#包括
使用名称空间std;
常数int MAXV=100;
int main()
{
int t;
cin>>t;
int n[t],g[t];//n[w]>>g[w];
对于(int i=0;i>stu[i];
}
}    


while(你说的尝试使用动态内存分配并在for loopDynamic.memory.allocation.stu外部声明stu[n[w]]用于为测试用例中的不同值n创建一个数组,正如你说的尝试使用动态内存分配并在for loopDynamic.memory.allocation.stu外部声明stu[n[w]]用于在测试用例中为n的不同值创建一个数组(我想g++?支持它作为扩展)@ShinTakezou正确,它是一个GCC扩展,可以通过严格的编译标志禁用。看到它了吗,用
-pedantic
尝试了一下,并相应地更新了答案。谢谢你,先生。因为我只是一个初学者,你能告诉我什么是向量,它与其他数据结构有何不同吗?@kartikhare欢迎你。看一看。(我想g++支持它作为扩展)@ShinTakezou正确,它是一个GCC扩展,可以通过严格的编译标志禁用。看到它了吗,用
-pedantic
尝试了一下,并相应地更新了答案。谢谢你,先生。因为我只是一个初学者,你能告诉我什么是向量,它与其他数据结构有何不同吗?@kartikhare欢迎你。看一看。(带有选项
-pedantic
的g++由于数组大小可变而出现错误)非常感谢,先生…它在我的编译器上运行良好,但是在输入第一个测试用例的值后,它不会覆盖数组中已存储的值以用于后面的情况吗(g++带有选项
-pedantic
由于数组大小可变而产生错误)非常感谢,先生…在我的编译器上运行良好,但是在输入第一个测试用例的值后,它不会覆盖数组中已存储的值,用于后一个测试用例吗