Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 哪个c++;容器可以包含C int多维数组(C+;+;03/MSVC11)?_C++_Multidimensional Array_Containers_C++03_Visual Studio 2012 - Fatal编程技术网

C++ 哪个c++;容器可以包含C int多维数组(C+;+;03/MSVC11)?

C++ 哪个c++;容器可以包含C int多维数组(C+;+;03/MSVC11)?,c++,multidimensional-array,containers,c++03,visual-studio-2012,C++,Multidimensional Array,Containers,C++03,Visual Studio 2012,我有两个多维数组 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,

我有两个多维数组

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};
#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";
    }
}
我收到这个错误:

error C2440: 'initializing' : cannot convert from 'int [68][8]' to 'int'
我想这不是正确的方法。。
推荐的方法是什么?

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

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}
    }
};
将元素(2D数组)作为单独的数组将意味着您无法将这些数组存储在另一个数组中;您只能存储指向它们的指针。使用这种方法,您可以使用这样的引用非常方便地引用内部元素

auto const &anArray1 = LevelsArray[0];

并在其他地方使用它。

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

int LevelsArray[LEVELS_COUNT][MAX_ROW][MAX_CELL] = { {...}, {...} };
<>但是,C++方式是使用或(要求C++ 11):

std::向量级数组=
{
{
{ 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::数组级别数组=
{
{
{ 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}
}
};

> >我不认为一个普通的数组是C++容器。我宁愿使用
std::vector
(或
std::array
)的组合,因为

下面是一个例子:

#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";
    }
}
改用
std::array
可能看起来像这样:

int LevelsArray[LEVELS_COUNT] = { anArray1, anArray2};
#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";
    }
}
#包括
#包括
#包括

  • 它们不是INT。尝试创建数组的typedef及其数组。
    LevelsArray
    的类型不应该是
    int[][][]
    ?简短回答:无。更详细的回答:可以实例化一个数组容器,但是你不能用它做任何有用的事情。
    int-LevelsArray[LEVELS\u COUNT][MAX\u ROW][MAX\u CELL]={anArray1,anArray2}不起作用,您必须给出此数组中的数组元素。std::array示例给出:错误C2078:初始值设定项过多errorvector示例给出错误:错误C2552:“LevelsArray”:无法使用visual studio中的初始值设定项列表初始化非聚合2012@user63898您需要对初始值设定项列表和std:array提供适当的C++11支持,使用C++03将更加复杂。对于类型为“levels”的对象,不允许使用“{…}”进行初始化在使用visual studio 2012的vector示例中,我还发现std::array示例的错误,例如:“level”:非法使用此类型作为expression@user63898代码> STD::数组成为C++ C++ 11标准的一部分。您可能需要检查编译器是否支持该功能,或者如何启用该功能。(我在示例中使用的基于范围的for循环也是如此。)我用g++4.8.3.oo测试了这两个示例。所以它不好,我需要一些支持旧编译器的东西(2008)  also@user63898你需要了解初始化代码< >代码::vector < /代码>而不使用初始化列表:有没有方法将数组复制到int数组,而不是使用C++ 11,而不是循环所有的级别数组?C++作为一种语言不支持用数组初始化数组。您必须手动复制它们,或者使用
    std::copy
    在幕后也可以这样做。如果你真的想要像你所要求的那样灵活的容器,也许可以试试
    std::vector
    。这个向量也会给我带来问题,请参阅上面的commnets,因为你需要C++11,其中
    vector
    获取一个
    std::initializer\u list
    获取构造函数。我认为VS 2012没有。
    #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";
        }
    }