设置整个数组的值的最简单方法是什么?

发布于 2024-09-05 13:25:13 字数 432 浏览 2 评论 0原文

我当前的项目要求我根据其他一些值填充一个数组。我知道有捷径:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撑一把青伞 2024-09-12 13:25:13

使用 std::copy() 怎么样?

int arr[4][4];
if(someothervariable == 1){
        const static int a2[4][4] = { {1,1,1,1},
                                      {1,2,3,4},
                                      {2,3,4,5},
                                      {3,4,5,6} };
        std::copy(&a2[0][0], &a2[0][0]+16, &arr[0][0]);
}

How about using std::copy() ?

int arr[4][4];
if(someothervariable == 1){
        const static int a2[4][4] = { {1,1,1,1},
                                      {1,2,3,4},
                                      {2,3,4,5},
                                      {3,4,5,6} };
        std::copy(&a2[0][0], &a2[0][0]+16, &arr[0][0]);
}
缘字诀 2024-09-12 13:25:13

不,数组初始化语法用于数组初始化。不过,如果所有值都是同一字节,则可以使用 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).

若水微香 2024-09-12 13:25:13

在当前版本的 C++ 语言中,唯一的方法是从一些原始版本中复制它。

int arr[4][4];

if (someothervariable == 1)
{
  const int SOURCE[4][4] = // make it `static` if you prefer
  { 
    {1, 1, 1, 1},
    {1, 2, 3, 4},
    {2, 3, 4, 5},
    {3, 4, 5, 6} 
  };

  assert(sizeof arr == sizeof SOURCE); // static assert is more appropriate
  memcpy(&arr, &SOURCE, sizeof arr);
}

源“常量”可以声明为 static 以避免重新初始化(如果编译器不支持)足够聪明,可以自行优化。

在该语言的未来版本中,计划推出类似于 C 复合文字的功能,它将提供对立即初始化的支持(基本上是您在原始帖子中尝试做的事情)。

In the current version of C++ language the only way to do it is to copy it from some original

int arr[4][4];

if (someothervariable == 1)
{
  const int SOURCE[4][4] = // make it `static` if you prefer
  { 
    {1, 1, 1, 1},
    {1, 2, 3, 4},
    {2, 3, 4, 5},
    {3, 4, 5, 6} 
  };

  assert(sizeof arr == sizeof SOURCE); // static assert is more appropriate
  memcpy(&arr, &SOURCE, sizeof arr);
}

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).

小巷里的女流氓 2024-09-12 13:25:13

如果您希望用单个值填充数组:

#include<algorithm>
#include<vector>

// ...
std::vector<int> arr;
std::fill(arr.begin(), arr.end(), VALUE);  // VALUE is an integer

如果您希望计算每个元素的值:

struct get_value {
    int operator()() const { /* calculate and return value ... */ }
};

std::generate(arr.begin(), arr.end(), get_value());

If you wish to fill the array with a single value:

#include<algorithm>
#include<vector>

// ...
std::vector<int> arr;
std::fill(arr.begin(), arr.end(), VALUE);  // VALUE is an integer

If you wish to calculate the value for each element:

struct get_value {
    int operator()() const { /* calculate and return value ... */ }
};

std::generate(arr.begin(), arr.end(), get_value());
两相知 2024-09-12 13:25:13

如果您将所有内容设置为相同的值(例如零),您可能能够逃脱……

memset (arr, 0, sizeof (arr));

请注意,这充满了危险。你必须了解你的字体大小和所有这些爵士乐。

然而,这似乎对你来说还不够。如果你想用不同的值填充数组,我只能想到两种方法。

方法#1。 (可能会让人很痛苦)

arr[0][0] = 1;
...
arr[0][3] = 1;
arr[1][0] = 1;
...
arr[1][3] = 4;
arr[2][0] = 2;
...
arr[2][3] = 5;
arr[3][0] = 3;
...
arr[3][3] = 6;

方法#2。
预定义一组数组并使用指针在它们之间切换;

int  arr1[4][4] = {
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0} };
int  arr2[4][4] = {
        {1,1,1,1},
        {1,2,3,4},
        {2,,3,4,5},
        {3,4,5,6} };

int *arr[4];

现在您只需设置 *arr[] 的四 (4) 个值,而不是设置所有内容。当然,只有当您的数组填充有预定常量时,这才真正有效。

希望这有帮助。

If you are setting everything to the same value (such as zero), you may be able to get away with ...

memset (arr, 0, sizeof (arr));

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)

arr[0][0] = 1;
...
arr[0][3] = 1;
arr[1][0] = 1;
...
arr[1][3] = 4;
arr[2][0] = 2;
...
arr[2][3] = 5;
arr[3][0] = 3;
...
arr[3][3] = 6;

Method #2.
Predefine a set of arrays and switch between them using a pointer;

int  arr1[4][4] = {
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0} };
int  arr2[4][4] = {
        {1,1,1,1},
        {1,2,3,4},
        {2,,3,4,5},
        {3,4,5,6} };

int *arr[4];

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.

沧桑㈠ 2024-09-12 13:25:13

我不确定我是否喜欢这个解决方案,但是如果您将数组包装在一个结构体中,那么 C/C++ 将为您提供赋值便利,而只需使用结构体名称来获取数组,成本很小:

typedef struct {
    int data[4][4];
} info_t;

info_t arr;

if (someothervariable == 1){
    static const info_t newdata = {{  // since this is static const, there generally
                                      // won't be a copy - that data will be 'baked' 
                                      // into the binary image (or at worst a
                                      // single copy will occur)
        {1,1,1,1},
        {1,2,3,4},
        {2,3,4,5},
        {3,4,5,6} 
    }};
    arr = newdata;  // easy to assign new data to the array
}

int somethingelse = arr.data[1][2]; // a tiny bit less convenient to get 
                                    //    to the array data

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:

typedef struct {
    int data[4][4];
} info_t;

info_t arr;

if (someothervariable == 1){
    static const info_t newdata = {{  // since this is static const, there generally
                                      // won't be a copy - that data will be 'baked' 
                                      // into the binary image (or at worst a
                                      // single copy will occur)
        {1,1,1,1},
        {1,2,3,4},
        {2,3,4,5},
        {3,4,5,6} 
    }};
    arr = newdata;  // easy to assign new data to the array
}

int somethingelse = arr.data[1][2]; // a tiny bit less convenient to get 
                                    //    to the array data
乖乖兔^ω^ 2024-09-12 13:25:13
int arr[4][4];
if (someothervariable == 1) {
     int tmp[4][4] = { {1, 1, 1, 1}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} };
     arr = tmp;
}
int arr[4][4];
if (someothervariable == 1) {
     int tmp[4][4] = { {1, 1, 1, 1}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} };
     arr = tmp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文