重新设置一个数组 [英] re-setting an array

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

问题描述

有没有办法用一条指令重置数组的所有元素?

我想将所有元素都设置为零。目前循环播放。


thx,

Scott Kelley

Is there a way to reset all elements of an array with a single instruction?
I want to set all elements to zero. Currently looping to do so.

thx,
Scott Kelley

推荐答案

>有没有办法用一条指令重置数组的所有元素?
> Is there a way to reset all elements of an array with a single instruction?
我想将所有元素设置为零。目前循环执行此操作。
I want to set all elements to zero. Currently looping to do so.



设置为零可以使用

void * memset(void * dest,int c,size_t count);


代码看起来像:

memset(intArray,0,sizeof(int)* arraySize);


非常快,但你必须照顾正确的字节数来设置

自己归零!


setting to zero could be done with
void *memset(void *dest, int c, size_t count);

the code could look like:
memset(intArray, 0, sizeof(int) * arraySize);

Very fast but you have to take care of the correct byte-count to be set
to zero yourself!


Scott Kelley写道:
Scott Kelley wrote:
有没有办法用一条指令重置数组的所有元素?
我想将所有元素设置为零。目前循环执行此操作。
Is there a way to reset all elements of an array with a single instruction?
I want to set all elements to zero. Currently looping to do so.




memset()在< cstring>中定义。


检查: http://www.cplusplus.com/ref/cstring/index.html


问候,


Ioannis Vranos



memset() defined in <cstring>.

Check this: http://www.cplusplus.com/ref/cstring/index.html


Regards,

Ioannis Vranos




" Scott Kelley" < SC **** @ iccom.com>在消息中写道

新闻:Kq ******************** @ centurytel.net ...

"Scott Kelley" <sc****@iccom.com> wrote in message
news:Kq********************@centurytel.net...
是有没有办法用一个
指令重置数组的所有元素?我想将所有元素设置为零。目前循环这样做。
Is there a way to reset all elements of an array with a single instruction? I want to set all elements to zero. Currently looping to do so.




如果你在谈论整数然后memset。


如果你在谈论浮点数然后是std :: fill或std :: fill_n。 std :: fill和std :: fill_n的优点是它们可以与许多不同的类型,值和数据结构一起使用,但是对于设置为
零,你不会比memset更有效率。


john



If you are talking about integers then memset.

If you are talking about floating point then std::fill or std::fill_n. The
advantage of std::fill and std::fill_n is that they work with many
different types, values and data structures, but for integer arrays set to
zero you aren''t going to get any more efficient then memset.

john


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

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