什么是设置一个值数组的所有元素的最好方法? [英] What's the best way to set all elements of array to a value?

查看:85
本文介绍了什么是设置一个值数组的所有元素的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有int数组,我想设置阵列中的所有值X每次一个函数被调用。

I have an array of ints and I'd like to set all values in the array to 'x' everytime a function is called.

我看着memset的,但是这只会因为我觉得一个字节数组工作。

I've looked at memset but that would only work for an array of bytes I think.

我可以做明显的循环,但我猜有一个标准库函数,在那里,将做到这一点更好。任何人都知道?

I could do the obvious for loop, but I'm guessing there is a standard lib function out there that will accomplish this better. Anyone know?

推荐答案

只要循环使用,pretty得多。或 memset的 0时,如果你知道值为零(对于您拥有比特重presentation知识的其他值类似)。不会有一个标准库溶液中,由于标准库无法知道特定的用户类型的

Just loop it, pretty much. Or memset to 0, if you know the value is zero (similar for other values for which you have knowledge of the bit representation). There won't be a standard lib solution, since the standard lib can't know of particular user types.

这篇关于什么是设置一个值数组的所有元素的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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