在声明之后为数组赋值 [英] assigning values to an array after declaring it

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

问题描述

非常基本的问题我很害怕。一旦声明了一个数组,是否会给这些成员分配值的繁琐方式比以下更简单:


myarray [0] = 8;

myarray [1] = 3;

myarray [2] = 4;

myarray [3] = 0;

myarray [4] = 0;

myarray [5] = 1;

myarray [6] = 8;

myarray [7 ] = 8;

等...


我无法在申报时立即分配价值,因为我想要myarray

根据各种情况进行不同的初始化,仅在运行时确定




感谢您的帮助。最好的问候-Eric

解决方案

Eric Bantock写道:

非常基本的问题我很害怕。一旦声明了数组,是否有一种不那么繁琐的方法为其成员分配值而不是以下内容:

myarray [0] = 8;
myarray [1] = 3;
myarray [2] = 4;
myarray [3] = 0;
myarray [4] = 0;
myarray [5] = 1;
myarray [6] = 8;
myarray [7] = 8;
等...

我不能立即在声明中分配值因为我想要myarray <根据仅在运行时确定的各种情况进行不同的初始化。

感谢您的帮助。最好的问候-Eric




以下是一些想法:

1.许多常量数组:

声明常量数组对于程序中的每个案例。

根据具体情况,将数据从常量数组​​复制到变量

。如果数据不会改变,那么

则使用指针而不是数组。


2.从流(文件)加载数据。

首选文本文件为二进制文件。可以使用编辑器轻松创建和更新文本文件

。二进制文件

更难以创建和维护。


3.从其他来源加载数据。

某些平台有地方程序可以存储他们的数据,例如注册表或ROM。访问这些

是这个新闻组的主题,而不是便携式的。

-

Thomas Matthews


C ++新闻组欢迎辞:
http:// www .slack.net / ~shiva / welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


2004年6月16日星期三15:55:26 +0000(UTC) ,在comp.lang.c中,Eric Bantock

< eb@example.com>写道:

非常基本的问题我很害怕。一旦声明了一个数组,是否有一种不那么繁琐的方法为其成员分配值

(比为成员分配值)


No.


好​​吧,如果你能确定字节顺序和

对齐约束,你可以将数据存入其中。


-

Mark McIntyre

CLC FAQ< http://www.eskimo.com/~scs/C-faq/ top.html>

CLC自述文件:< http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>

---- ==已发布通过Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News == ----
http: //www.newsfeed.com 世界排名第一的新闻组服务! > 100,000新闻组

--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---

---- ==通过新闻发布.Com - Unlimited-Uncensored-Secure Usenet News == ----
http:// www.newsfeed.com 世界排名第一的新闻组服务! > 100,000新闻组

--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---


Eric Bantock写道:

非常基本的问题我很害怕。一旦声明了数组,是否有一种不那么繁琐的方法为其成员分配值而不是以下内容:

myarray [0] = 8;
myarray [1] = 3;
myarray [2] = 4;
myarray [3] = 0;
myarray [4] = 0;
myarray [5] = 1;
myarray [6] = 8;
myarray [7] = 8;
等...

我不能立即在声明中分配值因为我想要myarray <根据仅在运行时确定的各种情况进行不同的初始化。




您可以使用循环:


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

myarray [i] = some_expression_probably_involving_i;


当然,这个方法要求有一些方法

落后于8,3,4的明显疯狂,...


如果阵列内容不系统或不容易计算

但是从十九世纪玻利维亚的钼产量的历史记录中已经费力地输入了*但是* th ere

只是少数这样的数据集,你可以随身携带一些预先生成的初始化数组,然后将选中的数据复制到

myarray []在运行时:


const SomeType data_sets [] [N] = {

{8,3,4,...} ,

{6,6,8,...},

{4,9,2,...},

。 ..

};

SomeType myarray [N];

...

int k = index_of_chosen_data_set;

memcpy(myarray,data_sets [k],sizeof myarray);


很难推荐一种方法而不是另一种方法(或者

替代可能性)没有一些你想要实现的
的概念。


-
< a href =mailto:Er ********* @ sun.com> Er ********* @ sun.com


Very basic question I''m afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:

myarray[0]=8;
myarray[1]=3;
myarray[2]=4;
myarray[3]=0;
myarray[4]=0;
myarray[5]=1;
myarray[6]=8;
myarray[7]=8;
etc...

I can''t assign values immediately on declaration because I want myarray
to be initialised differently according to various cases only determined
at runtime.

Thanks for any help. best regards -Eric

解决方案

Eric Bantock wrote:

Very basic question I''m afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:

myarray[0]=8;
myarray[1]=3;
myarray[2]=4;
myarray[3]=0;
myarray[4]=0;
myarray[5]=1;
myarray[6]=8;
myarray[7]=8;
etc...

I can''t assign values immediately on declaration because I want myarray
to be initialised differently according to various cases only determined
at runtime.

Thanks for any help. best regards -Eric



Here are some ideas:
1. Many constant arrays:
Declare constant arrays for each case in the program.
Copy the data from the constant array to the variable
depending on the circumstances. If the data won''t change,
then use a pointer instead of an array.

2. Load data from a stream (file).
Prefer a text file to a binary file. Text files can be
easily created and updated using an editor. Binary files
are more difficult to create and maintain.

3. Load data from another source.
Some platforms have places that programs can store
their data, such as a registry or ROM. Accessing these
are off-topic for this newsgroup and not portable.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


On Wed, 16 Jun 2004 15:55:26 +0000 (UTC), in comp.lang.c , Eric Bantock
<eb@example.com> wrote:

Very basic question I''m afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members


(than assigning values to members)

No.

Well, you could memcpy data into it, if you could be sure of byte order and
alignment constraints.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Eric Bantock wrote:

Very basic question I''m afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:

myarray[0]=8;
myarray[1]=3;
myarray[2]=4;
myarray[3]=0;
myarray[4]=0;
myarray[5]=1;
myarray[6]=8;
myarray[7]=8;
etc...

I can''t assign values immediately on declaration because I want myarray
to be initialised differently according to various cases only determined
at runtime.



You can use a loop:

for (i = 0; i < N; ++i)
myarray[i] = some_expression_probably_involving_i;

Of course, this approach requires that there is some method
behind the apparent madness of 8,3,4,...

If the array contents are not systematic or readily computed
but have been laboriously typed in from historical records of
molybdenum production in nineteenth-century Bolivia *but* there
are only a few such sets of data, you could carry a few pre-
initialized arrays around and then copy the chosen one into
myarray[] at run-time:

const SomeType data_sets[][N] = {
{ 8,3,4, ... },
{ 6,6,8, ... },
{ 4,9,2, ... },
...
};
SomeType myarray[N];
...
int k = index_of_chosen_data_set;
memcpy(myarray, data_sets[k], sizeof myarray);

It''s hard to recommend one approach over the other (or
over alternative possibilities) without some notion of what
you''re really trying to accomplish.

--
Er*********@sun.com


这篇关于在声明之后为数组赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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