静态标注数组的规则 [英] Rules for statically dimensioning arrays

查看:72
本文介绍了静态标注数组的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回到旧C的时代,只有数字文字可用作静态分配数组的尺寸 - 尺寸必须是

解析为常数/编译前。现在我开始怀疑(!)这不再是C或C ++的情况:(


任何人都可以升级我目前的状态/版本的规则?(对于

C ++)


确切地说,我回溯了多少年了?


Samee

解决方案

在c ++中你可以使用stl容器 - 当使用push_back时不需要

预先分配内存,请参阅
http://www.sgi.com/tech/stl/Vector.html


- #include< iostream>

- #include< vector>

-

-使用命名空间std;

-

-int main(void)

- {

- vector< int> v;

-

- cout<< v.capacity()<< endl;

- cout<< v.size()<< endl;

-

- v.push_back(1);

-

- cout<< v.capacity()<< endl;

- cout<< v.size()<<结束;

-

- v.reserve(25);

- cout<< v.capacity()<< endl;

- cout<< v.size()<< endl;

-

- 返回0;

- }


< blockquote> Samee Zahur写道:

在旧C的时代,只有数字文字可用作静态分配数组的尺寸 - 尺寸必须为
在编译时/之前解析为常量。现在我开始怀疑(!)用C或C ++不再是这种情况:(


对于C,它不再是真的。

任何人都可以使用当前状态/版本的规则升级我吗?(对于
C ++)


仍然相同。静态分配的大小数组必须是编译时间

常量。

确切地说,我回溯了多少年???




我不知道。


" Samee Zahur"< sa ********* @ gmail。 com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

在旧C的时代,只有数字文字可用作静态分配数组的维度 - 大小必须在编译时/之前解析为常量。现在我开始怀疑(!)用C或C ++不再是这种情况:(

任何人都可以用规则的现状/版本打我? (对于
C ++)
这对于C ++来说还没有改变(因为C ++是从ANSI C''90

标准派生的)。但是C ++提供标准库容器作为首选

意味着存储动态大小的集合。

确切地说,我回溯了多少年???



1999年,ANSI / ISO C标准的更新引入了运行时大小的数组
。例如:

void f(int n)

{

char storage [n] = {0}; //在运行时定义的大小

}

C ++的等价物是:

std :: vector< char>存储(i);

它有几个好处,能够调整集合的大小,以及在
$的情况下明确定义的行为b $ b内存不足错误 - 以及其他专业人员。

这就是说,为了保持与C的紧密兼容性,下一个C ++标准可能是b
包含C语言的这个新的

功能。

问候,

Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格


Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I''m beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

Can anyone upgrade me with the present state/version of rules? (for
C++)

Exactly, by how many years am I back-dated ???

Samee

解决方案

in c++ you can use stl containers - when use push_back there''s no need
to allocate memory beforehand, see as well e.g.
http://www.sgi.com/tech/stl/Vector.html

-#include <iostream>
-#include <vector>
-
-using namespace std;
-
-int main(void)
-{
- vector<int> v;
-
- cout << v.capacity() << endl;
- cout << v.size() << endl;
-
- v.push_back(1);
-
- cout << v.capacity() << endl;
- cout << v.size() << endl;
-
- v.reserve(25);
- cout << v.capacity() << endl;
- cout << v.size() << endl;
-
- return 0;
-}


Samee Zahur wrote:

Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I''m beginning to
suspect(!) that this is no longer the case either with C or with C++ :(
For C, it is no longer true.
Can anyone upgrade me with the present state/version of rules? (for
C++)
Still the same. sizes of statically allocated arrays must be compile-time
constants.
Exactly, by how many years am I back-dated ???



I don''t know.


"Samee Zahur" <sa*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I''m beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

Can anyone upgrade me with the present state/version of rules? (for
C++) This has not changed for C++ yet (as C++ is derived from the ANSI C ''90
standard). But C++ offers standard library containers as a preferred
means to store dynamically sized collections.
Exactly, by how many years am I back-dated ???


In 1999, an update to the ANSI/ISO C standard has introduced
run-time sized arrays. E.g.:
void f(int n)
{
char storage[n] = {0}; // size defined at run time
}
The C++ equivalent is:
std::vector<char> storage(i);
It has several benefits, in terms of being able to resize
the collection, and well-defined behavior in case of an
out-of-memory error -- among other pros.
This said, to maintain close compatibility with C, it is
likely that the next C++ standard will incorporate this new
feature of the C language.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


这篇关于静态标注数组的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆