关于数组的问题.. [英] Question about arrays..

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

问题描述

我是C ++语法的新手。


我知道数组可以像这样进行delcared:

int a [] = { 1,2,3,4};


但如果我希望数组是全局的,然后设置长度

和方法?


就像在java中一样,你可以这样做:


int a [];

void main( )

{

a = new int {1,2,3,4};

}


我如何用C ++做到这一点?

I''m sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

推荐答案

文章< 11 ********** ***********@m73g2000cwd.googlegroups。 com>,
ne ************* @ gmail .com 说...
In article <11*********************@m73g2000cwd.googlegroups. com>,
ne*************@gmail.com says...

我是C ++语法的新手。


我知道数组可以像这样进行delcared:

int a [] = {1,2,3,4};


但如果我想要数组是一个全局的,然后在一个方法中设置长度

和值?
I''m sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?



你不能这样做(不是直接)。如果你想这样做,你可能会更好地使用std :: vector而不是



-

后来,

杰瑞。


宇宙是自己想象的虚构。

You can''t do it (not directly anyway). If you want to do that, you''re
probably better off using an std::vector instead.

--
Later,
Jerry.

The universe is a figment of its own imagination.


我明白了......我会看一下,非常感谢

I see... I''ll look that up, thanks alot


就像在java中一样,你可以这样做:
like in java, you can do:

>

int a [];

void main()

{

a = new int {1,2,3,4};

}


如何在C ++中执行此操作?
>
int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?



没有直接的方法,但你可以试试这个..


int * a; //全球声明


无效方法()

{

a = new int [size];

for(i = 0 ; i< size; i ++)

a [i] = value;

}

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a[i] = value ;
}


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

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