【问题标题】:Which c++ container can contain C int multidimensional arrays (C++03/MSVC11)?哪个 c++ 容器可以包含 C int 多维数组 (C++03/MSVC11)?
【发布时间】:2023-04-02 03:37:01
【问题描述】:

我有这个多维 2 数组

int anArray1[MAX_ROW][MAX_CELL] =
{
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 0, 0, 0, 0, 0, 0, 0}
}


int anArray2[MAX_ROW][MAX_CELL] =
{
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 1, 1, 0, 1, 1, 1, 0},
    { 0, 0, 0, 0, 0, 0, 0, 0}
}

我想将它们存储在基于索引的容器中,我尝试制作另一个应该像这样保存它们的 int 数组:

int LevelsArray[LEVELS_COUNT] = { anArray1, anArray2};

我收到此错误:

error C2440: 'initializing' : cannot convert from 'int [68][8]' to 'int'

我猜这不是正确的方法.. 推荐的方式是什么?

【问题讨论】:

  • 它们不是整数。尝试制作一个数组的 typedef 和一个数组。
  • LevelsArray 的类型不应该是int[][][]吗?
  • 简短回答:无。更长的答案:可以实例化一个数组容器,但你将无法用它做任何有用的事情。

标签: c++ multidimensional-array containers c++03 visual-studio-2012


【解决方案1】:

我不会将普通的旧数组视为 C++ 容器。我宁愿使用std::vector(或std::array)的组合,因为reasons

这是一个例子:

#include <iostream>
#include <vector>

typedef int cell;
typedef std::vector<cell> row;
typedef std::vector<row> level;
typedef std::vector<level> levels;

int main() {
    levels l = 
    {
        {
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0}
        },
        {
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 1, 1, 0, 1, 1, 1, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0}
        },
    };
    for (int i = 0; i < l.size(); i++) {
        std::cout << "level " << i << ":\n" ;
        for (row & r : l[i]) {
            for (cell & c : r)
                std::cout << c << " ";
            std::cout << "\n";
        }
        std::cout << "\n";
    }
}

输出是:

$ g++ test.cc -std=c++11 && ./a.out
level 0:
0 1 1 0 1 1 1 0 
0 1 1 0 1 1 1 0 
0 1 1 0 1 1 1 0 
0 1 1 0 1 1 1 0 
0 0 0 0 0 0 0 0 

level 1:
0 1 1 0 1 1 1 0 
0 1 1 0 1 1 1 0 
0 1 1 0 1 1 1 0 
0 1 1 0 1 1 1 0 
0 0 0 0 0 0 0 0 

使用std::array 可能看起来像这样:

#include <iostream>
#include <vector>
#include <array>

const int cells_per_row = 8;
const int rows_per_level = 5;
const int nlevels = 2;
typedef int cell;
typedef std::array<cell, cells_per_row> row;
typedef std::array<row, rows_per_level> level;
typedef std::array<level, nlevels> levels;

int main() {
    levels l = 
    {
        level{
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 0, 0, 0, 0, 0, 0, 0}
        },
        level{
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 1, 1, 0, 1, 1, 1, 0},
            row{ 0, 0, 0, 0, 0, 0, 0, 0}
        },
    };
    for (int i = 0; i < l.size(); i++) {
        std::cout << "level " << i << ":\n" ;
        for (row & r : l[i]) {
            for (cell & c : r)
                std::cout << c << " ";
            std::cout << "\n";
        }
        std::cout << "\n";
    }
}

请注意,这些示例使用了 C++11 功能(初始化列表、基于范围的 for、std::array),这些功能可能并非每个编译器都支持。虽然您可以使用初始化器列表来初始化容器和基于范围的 for 循环以进行打印,但在 C++11 之前没有 std::array。

供参考:

【讨论】:

  • 在使用 Visual Studio 2012 的矢量示例中,“级别”类型的对象不允许使用“{...}”进行初始化
  • 还给我一些 std::array 示例的错误,例如:'level':非法使用这种类型作为表达式
  • @user63898 std::array 成为具有 C++11 标准的 C++ 的一部分。您可能需要检查您的编译器是否支持该功能或如何启用它。 (我在示例中使用的基于范围的 for 循环也是如此。)我使用 g++ 4.8.3 测试了这两个示例。
  • oo 。所以它不好我还需要支持旧编译器(2008)的东西
  • @user63898 您需要了解在不使用初始化列表的情况下初始化std::vector 的选项:stackoverflow.com/q/2236197/1025391
【解决方案2】:

您可以使用 LevelsArray 类型的更明确的声明

int LevelsArray[LEVELS_COUNT][MAX_ROW][MAX_CELL] = { {...}, {...} };

但是 C++ 的方式是使用 std::vectorstd::array(需要 C++11):

std::vector<std::vector<std::vector<int>>> LevelsArray =
{
    {
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0}
    },
    {
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0}
    }
};

std::array<std::array<std::array<int, MAX_CELL>, MAX_ROW>, LEVELS_COUNT> LevelsArray =
{
    {
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0}
    },
    {
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0}
    }
};

【讨论】:

  • int LevelsArray[LEVELS_COUNT][MAX_ROW][MAX_CELL] = { anArray1, anArray2}; 不起作用,您必须在此数组中提供数组元素。
  • std::array 示例给了我 :error C2078: too many initializers 错误
  • vector 示例给出错误:错误 C2552: 'LevelsArray' : non-aggregates 无法在 Visual Studio 2012 中使用初始化列表进行初始化
  • @user63898 你需要适当的 C++11 支持初始化列表和 std:array,使用 C++03 会更复杂。
【解决方案3】:

您没有指定容器的其他维度,这取决于所包含的元素。试试这个

int LevelsArray[][MAX_ROW][MAX_CELL] =
{
    {
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0}
    },
    {
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 1, 1, 0, 1, 1, 1, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0}
    }
};

将元素(二维数组)作为单独的数组意味着您不能将这些数组存储在另一个数组中;您只能存储指向它们的指针。使用这种方法,您可以使用类似这样的引用来非常方便地引用内部元素

auto const &anArray1 = LevelsArray[0];

并在其他地方使用它。

【讨论】:

  • 有没有办法将数组复制到 int 数组而不使用 c++11 并且不循环所有 Level 数组?
  • C++ 作为一种语言不支持用数组初始化数组。您必须手动复制它们或使用std::copy,这也将在幕后做同样的事情。如果你真的想要像你问的那样灵活的容器,也许可以试试std::vector
  • 向量也让我有问题,见上面的commnets
  • 为此,您需要 C++11,其中 vector 获取 std::initializer_list 采用构造函数。我认为 VS 2012 没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
相关资源
最近更新 更多