C ++有关堆栈动态数组 [英] C++ about dynamic array on stack
问题描述
据我所知,C ++ / C doen't堆栈支持动态数组。
在以下delcaration:
int数据[N]; //如果n不在编译时间决定,这将导致错误
但最近,我读了一些其他球员code如下:
// **
看来n个不能compling时间来决定,但是当我运行它,如果我fprintf中形成的,每次我得到了正确的数组大小!!!!!!
在G ++版本是4.7.1
这是因为G ++ 4.7.1支持C ++ 11×允许动态数组?
** //#包括LT&;&cstdio GT;
#包括LT&;&算法GT;
使用命名空间std;#包括LT&;&stdio.h中GT;个char [31];INT散列()
{
INT总和= 0;
对(INT I = 0,K = 0; K&下; 7;我+ +)
{
如果(S [I]≥='0'和;&放大器; S [1] - ='9')
{
总之* = 10; k ++;
总和+ =(S [I] - '0');
}
否则,如果(S [I]> ='A'和;&安培; S [1] - ;'Z')
{
总之* = 10; k ++;
总和+ =((S [I] - 'A' - (S [I]≥Q))/ 3 + 2);
}
}
返回总和;
}诠释的main()
{ INT N; scanf函数(%d个,&安培; N);
int数据[N];的getchar();
// fprintf中(标准错误,大小为%d \\ n,sizeof的(数据)/ sizeof的(数据[0]));
// **
看来n个不能compling时间来决定,但是当我运行它,如果我fprintf中形成的,每次我得到了正确的数组大小!!!!!!
* //
对于(INT TMP = 0; TMP< N; TMP ++)
{
得到(S);
数据[TMP =散列();
}
排序(数据,数据+ N);
布尔P = FALSE; N--;
的for(int i = 0,NUM = 1; I< N,I + = NUM = 1)
{
而(数据由[i] ==数据[I + 1])
{
NUM ++;
我++;
}
如果(NUM→1)
{
的printf(%03d-%04D%d个\\ N,数据[I] / 10000,数据[I]%10000,NUM);
P =真实的;
}
}
如果(对!)的printf(不重复\\ n);
返回0;
}
这些被称为的变长数组的(VLA),是一个G ++编译器扩展这不是C ++标准的一部分。用C,如果你希望你的code可移植的,不要用它++。
您可以 G ++
发出与 -Wvla
编译标志警告,或者标志的误差 = -Werror VLA
。我通常 -pedantic-错误
,它捕获这个和其他许多偏离标准的编译。
So far as I know,C++/C doen't support dynamic array on stack . In the following delcaration :
int data[n] ; // if the n is not decided at compiling time ,this leads to error
But recently , I read some other guys code as following:
//**
It seems the n number can not be decided at compling time,but when I run it , if i fprintf the formation, each time i got the correct array size !!!!!!
the G++ version is 4.7.1
Is this because the G++ 4.7.1 support C++11 x which allow dynamic array?
**//
#include <cstdio>
#include <algorithm>
using namespace std;
#include <stdio.h>
char s[31];
int Hash()
{
int sum=0;
for(int i=0,k=0;k<7;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
sum*=10;k++;
sum+=(s[i]-'0');
}
else if(s[i]>='A'&&s[i]<'Z')
{
sum*=10;k++;
sum+=((s[i]-'A'-(s[i]>'Q'))/3+2);
}
}
return sum;
}
int main()
{
int n;scanf("%d",&n);
int data[n];getchar();
//fprintf(stderr,"size is %d\n",sizeof(data)/sizeof(data[0]));
//**
It seems the n number can not be decided at compling time,but when I run it , if i fprintf the formation, each time i got the correct array size !!!!!!
*//
for(int tmp=0;tmp<n;tmp++)
{
gets(s);
data[tmp]=Hash();
}
sort(data,data+n);
bool p=false;n--;
for(int i=0,num=1;i<n;i+=num=1)
{
while(data[i]==data[i+1])
{
num++;
i++;
}
if(num>1)
{
printf("%03d-%04d %d\n",data[i]/10000,data[i]%10000,num);
p=true;
}
}
if(!p)printf("No duplicates.\n");
return 0;
}
These are called variable length arrays (VLA) and are a g++ compiler extension which is not part of the C++ standard. Do not use it in C++ if you want your code to be portable.
You can make g++
emit a warning with the -Wvla
compilation flag, or an error with flag -Werror=vla
. I usually compile with -pedantic-errors
, which catches this and many other deviations from the standard.
这篇关于C ++有关堆栈动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!