临时数组赋值 (C++)

发布于 2025-01-11 09:15:41 字数 595 浏览 4 评论 0原文

int arr[2][6] = {{0,0,0,1,1,1},
                 {2,2,2,3,3,3}};

for(int row=0;row<2;row++)
{
    for(int col=0;col<6;col++)
    {
        int temp[] = arr[row][col+4],arr[row][col+5],arr[row+1][col],arr[row+1][col+1];
        for(int i=0;i<1;i++)
        {
            cout << temp[i] << endl;
            //Replace 4 values in arr[] with values from temp[]
        }
    }
}

我正在尝试创建一个较小的临时数组,以便在更新原始二维数组时从中提取值。我收到以 int temp[] 开头的行错误。错误如下:

聚合对象预计使用“{...}”进行初始化

我不确定这里出了什么问题,我是 C++ 的初学者。

int arr[2][6] = {{0,0,0,1,1,1},
                 {2,2,2,3,3,3}};

for(int row=0;row<2;row++)
{
    for(int col=0;col<6;col++)
    {
        int temp[] = arr[row][col+4],arr[row][col+5],arr[row+1][col],arr[row+1][col+1];
        for(int i=0;i<1;i++)
        {
            cout << temp[i] << endl;
            //Replace 4 values in arr[] with values from temp[]
        }
    }
}

I'm trying to create a smaller temporary array to pull values from when updating the original 2D array. I'm getting an error with the line starting with int temp[]. The error reads:

initialization with '{...}' expected for aggregate object

I'm not sure what's going wrong here, I am a beginner with C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

森林迷了鹿 2025-01-18 09:15:41

您正在尝试使用单个 int 初始化一个 int[] 数组。您在数组的初始化值周围缺少大括号(请参阅其他数组上的大括号),例如:

int temp[] = { arr[row][col+4], arr[row][col+5], arr[row+1][col], arr[row+1][col+1] };

You are trying to initialize an int[] array with single int. You are missing curly braces around the array's initialization values (see the braces on your other array), eg:

int temp[] = { arr[row][col+4], arr[row][col+5], arr[row+1][col], arr[row+1][col+1] };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文