设置整个数组的值的最简单方法是什么?
我当前的项目要求我根据其他一些值填充一个数组。我知道有捷径:
int arr[4][4] = { {0,0,0,0} , {0,0,0,0} , {0,0,0,0} , {0,0,0,0} };
但在这种情况下,我需要在声明后填充数组。我目前的代码格式如下:
int arr[4][4];
if(someothervariable == 1){
arr = { {1,1,1,1},
{1,2,3,4},
{2,,3,4,5},
{3,4,5,6} };
}
但它无法编译。有没有办法在我的情况下使用提到的快捷方式?如果没有,最好的修复方法是什么?我希望有一种无需显式分配每个元素即可设置它的方法?即: arr[0][0] = ...
My current project requires me to fill an array based upon some other values. I know there's the shortcut:
int arr[4][4] = { {0,0,0,0} , {0,0,0,0} , {0,0,0,0} , {0,0,0,0} };
But in this case, I need to fill the array after its declaration. I currently have my code formatted like this:
int arr[4][4];
if(someothervariable == 1){
arr = { {1,1,1,1},
{1,2,3,4},
{2,,3,4,5},
{3,4,5,6} };
}
But it won't compile. Is there a way to make use of the mentioned shortcut in my case? If not, whats the best fix available? I'd appreciate a way to set it without explicitly assigning each element? ie: arr[0][0] = ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 std::copy() 怎么样?
How about using std::copy() ?
不,数组初始化语法用于数组初始化。不过,如果所有值都是同一字节,则可以使用 memset。
boost.assign 库添加了一些有趣的语法来修改/填充集合,但据我所知它不支持 C 样式数组(仅支持 C++ 和 Boost 容器)。
No, array initialization syntax is for array initialization. Although, you can use memset if all the values are the same byte.
The boost.assign library adds some interesting syntax for modifying/filling collections, but AFAIK it doesn't support C style arrays (only C++ and Boost containers).
在当前版本的 C++ 语言中,唯一的方法是从一些原始版本中复制它。
源“常量”可以声明为
static
以避免重新初始化(如果编译器不支持)足够聪明,可以自行优化。在该语言的未来版本中,计划推出类似于 C 复合文字的功能,它将提供对立即初始化的支持(基本上是您在原始帖子中尝试做的事情)。
In the current version of C++ language the only way to do it is to copy it from some original
The source "constant" can be declared as
static
in order to avoid re-initialization, if the compiler is not smart enough to optimize it by itself.In the future version of the language a feature similar to C's compound literals is planned, which will provide support for immediate initialization (basically what you tried to do in your original post).
如果您希望用单个值填充数组:
如果您希望计算每个元素的值:
If you wish to fill the array with a single value:
If you wish to calculate the value for each element:
如果您将所有内容设置为相同的值(例如零),您可能能够逃脱……
请注意,这充满了危险。你必须了解你的字体大小和所有这些爵士乐。
然而,这似乎对你来说还不够。如果你想用不同的值填充数组,我只能想到两种方法。
方法#1。 (可能会让人很痛苦)
方法#2。
预定义一组数组并使用指针在它们之间切换;
现在您只需设置 *arr[] 的四 (4) 个值,而不是设置所有内容。当然,只有当您的数组填充有预定常量时,这才真正有效。
希望这有帮助。
If you are setting everything to the same value (such as zero), you may be able to get away with ...
Note that this is fraught with perils. You have to know your type sizes and all that jazz.
However, it appears that that will not suffice for you. If you want to fill the array with different values, I can only only think of two ways of doing this.
Method #1. (Can be a pain the butt)
Method #2.
Predefine a set of arrays and switch between them using a pointer;
Now you only have the four (4) values of *arr[] to set instead of setting everything. Of course, this really only works if your arrays will be filled with predetermined constants.
Hope this helps.
我不确定我是否喜欢这个解决方案,但是如果您将数组包装在一个结构体中,那么 C/C++ 将为您提供赋值便利,而只需使用结构体名称来获取数组,成本很小:
I'm not sure if I like this solution or not, but C/C++ will give you assignment convenience if you wrap the array inside a struct with the minor cost of then having to use the struct name to get at the array: