迭代一个大小未知的数组? [英] Iterating through an array whose size is unknown?

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

问题描述

我只需要向一个函数发送一个数组,然后需要通过数组中的每个元素来获得
。我尝试使用sizeof(数组)/

sizeof(array [0])但是由于数组传递给函数,

sizeof(数组)实际上是sizeof(指针)到阵列中的第一个元素)和

因此无法解决我的问题。如果我无法计算数组的大小,我可以在不知道大小的情况下查看元素吗?

以某种方式测试我是否已经结束了数组?

I need to send just an array to a function which then needs to go
through each element in the array. I tried using sizeof(array) /
sizeof(array[0]) but since the array is passed into the function,
sizeof(array) is really sizeof(pointer to first element in array) and
therefore doesn''t solve my problem. If I can''t calculate the size of
the array, can I go through the elements without knowing the size and
somehow test whether I''m off the end of the array?

推荐答案

2004年7月14日21:30:03 -0700, ma *********** @ yahoo.com (matthurne)

在comp.lang.c ++中写道:
On 14 Jul 2004 21:30:03 -0700, ma***********@yahoo.com (matthurne)
wrote in comp.lang.c++:
我只需要向一个函数发送一个数组,然后需要通过数组中的每个元素。我尝试使用sizeof(array)/
sizeof(array [0]),但由于数组传递给函数,
sizeof(数组)实际上是sizeof(指向数组中第一个元素的指针)和<因此,并没有解决我的问题。如果我无法计算数组的大小,我是否可以在不知道大小的情况下浏览元素并以某种方式测试我是否已经离开数组的末尾了?
I need to send just an array to a function which then needs to go
through each element in the array. I tried using sizeof(array) /
sizeof(array[0]) but since the array is passed into the function,
sizeof(array) is really sizeof(pointer to first element in array) and
therefore doesn''t solve my problem. If I can''t calculate the size of
the array, can I go through the elements without knowing the size and
somehow test whether I''m off the end of the array?




但你确实知道了数组的大小,毕竟你创建了

数组。


那里如果你坚持使用数组代替

std :: vector,有两种可能性:


1.将数组的大小作为附加参数传递给

函数。


2.如果数组中的数据允许,有一个虚拟的标记值

告诉被叫它已到达数组末尾的函数。

这就是C库字符串函数的工作方式,''\ 0''表示结束


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq /top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu。 edu /~a ... FAQ-acllc.html



But you do know the size of the array, after all you created the
array.

There are two possibilities if you insist using an array instead of a
std::vector:

1. Pass the size of the array as an additional parameter to the
function.

2. If the data in the array permits, have a dummy sentinel value that
tells the called function that it has reached the end of the array.
That is how the C library string functions work, a ''\0'' marks the end
of the string.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


2004年7月14日21:30:03 -0700,matthurne< ma *** ********@yahoo.com>写道:
On 14 Jul 2004 21:30:03 -0700, matthurne <ma***********@yahoo.com> wrote:
我需要只发送一个数组到一个函数,然后需要通过数组中的每个元素。我尝试使用sizeof(array)/
sizeof(array [0]),但由于数组传递给函数,
sizeof(数组)实际上是sizeof(指向数组中第一个元素的指针)和<因此,并没有解决我的问题。如果我无法计算数组的大小,我是否可以在不知道大小的情况下浏览元素并以某种方式测试我是否已经离开数组的末尾了?
I need to send just an array to a function which then needs to go
through each element in the array. I tried using sizeof(array) /
sizeof(array[0]) but since the array is passed into the function,
sizeof(array) is really sizeof(pointer to first element in array) and
therefore doesn''t solve my problem. If I can''t calculate the size of
the array, can I go through the elements without knowing the size and
somehow test whether I''m off the end of the array?




不,你不能,你有两个选择


1)可怜的勒芒选项,将数组的大小传递给原来的

函数,所以你可以把它传递给后面的函数。


2)正确的C ++选项,使用向量而不是数组。矢量随身携带

它们的大小。在其他方面,矢量可以做你想做的事情。

希望数组能做到。它们还有许多其他有用的功能,例如

能够动态增长。你还没有学过C ++编程

如果你还没有学过矢量。阅读一本体面的C ++书。


john



No you cannot, you have two options

1) The poor mans option, pass the size of the array into the original
function, so you can pass it on to the later function.

2) The proper C++ option, use a vector instead of an array. Vectors carry
their size with them at all times. In others words vectors do what you are
hoping that arrays do. They also have lots of other useful features like
being able to grow dynamically. You haven''t really learnt C++ programming
if you haven''t learned about vectors. Read a decent C++ book.

john


" Jack Klein" < JA ******* @ spamcop.net>在消息中写道
"Jack Klein" <ja*******@spamcop.net> wrote in message
1.将数组的大小作为附加参数传递给
函数。

2.如果数据在数组允许,有一个伪的sentinel值,告诉被调用函数它已到达数组的末尾。
这就是C库字符串函数的工作方式,''\ 0''标记字符串的结尾。
1. Pass the size of the array as an additional parameter to the
function.

2. If the data in the array permits, have a dummy sentinel value that
tells the called function that it has reached the end of the array.
That is how the C library string functions work, a ''\0'' marks the end
of the string.




此外argv [argc]有效且指向NULL。


int main(int argc,char * * argv){

char * * iter = argv;

while(* iter)++ iter;

断言(iter-argv == argc); //应该是真的

argv [argc]; // ok,返回NULL

argv [argc + 1]; //内存访问冲突!

}

同样适用于许多编译器支持的扩展char * * env扩展。


int main(int argc,char * * argv,char * * env);


方法(1)和(2)的组合是第一个元素在

数组表示后面的元素数。因此,对于3个元素的数组,可能会有{3,7,

2,4}。数组中的第一个元素就像

一个哨兵,说明数组中的元素数量。但是,当然,这种方法可能并不总是可行,但需要考虑。



Also argv[argc] is valid and points to NULL.

int main(int argc, char * * argv) {
char * * iter = argv;
while (*iter) ++iter;
assert(iter-argv == argc); // should be true
argv[argc]; // ok, returns NULL
argv[argc+1]; // memory access violation!
}

Same holds for the extension char * * env extension many compilers support.

int main(int argc, char * * argv, char * * env);

A combination of methods (1) and (2) is to have the first element in the
array denote the number of elements following. Thus one would have { 3, 7,
2, 4 } for an array of 3 elements. This first element in the array is like
a sentinel saying the number of elements in the array. But of course, this
approach may not always be feasible, but it is something to consider.


这篇关于迭代一个大小未知的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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