Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++ Can';不要创建数组2d_C++ - Fatal编程技术网

C++ Can';不要创建数组2d

C++ Can';不要创建数组2d,c++,C++,我只想创建一个二维数组并为其赋值。我怎样才能解决这个问题? (我在评论中提到了错误。) 使用名称空间std; 类数组{ 私人: size\u t m;//错误:非静态数据成员“array::m”的使用无效| size\u t n;//错误:非静态数据成员“array::n”的使用无效| 整数数组[m][n]; 公众: 数组():m(0),n(0){ } 数组(常量int&m,常量int&n):m(m),n(n){ } //方法集数组 void setArray(){ for(size_t row

我只想创建一个二维数组并为其赋值。我怎样才能解决这个问题? (我在评论中提到了错误。)

使用名称空间std;
类数组{
私人:
size\u t m;//错误:非静态数据成员“array::m”的使用无效|
size\u t n;//错误:非静态数据成员“array::n”的使用无效|
整数数组[m][n];
公众:
数组():m(0),n(0){
}
数组(常量int&m,常量int&n):m(m),n(n){
}
//方法集数组
void setArray(){
for(size_t row=0;rowarray[row][col];//错误:在“[”标记之前应该有主表达式
}
}
}
};

在您的程序中,m和n是非常量,这意味着它们可以在程序的整个生命周期中发生变化

如果使用数组[m][n]语法声明数组,则m和n必须是常量。这里有两个选项:使m和n常量,或使数组动态(可以更改大小)。目前最简单的方法是使m和n常量:

using namespace std;

class array
{
    private:
        const size_t m = 10;
        const size_t n = 10;
        int array[m][n];  
    public:
        array() {}
        // method set array
        void setArray(){
            for (size_t row = 0; row < m; row++){
                for (size_t col = 0; col < n; col++){
                    cin>>array[row][col]; //error: expected primary-expression before '[' token
                }
            }
        }
};
使用名称空间std;
类数组
{
私人:
常数大小m=10;
常数大小n=10;
整数数组[m][n];
公众:
数组(){}
//方法集数组
void setArray(){
用于(行大小=0;行>数组[row][col];//错误:在“[”标记之前应该有主表达式
}
}
}
};
使数组动态化需要了解动态内存分配、指针、堆栈和堆以及new和delete操作符


为什么不直接使用
std::vector
std::vector
,这样它就可以为您管理这些东西了?

简单的方法是使用
vector

#include <vector>
#include <iostream>

class array {
private:
    std::vector<std::vector<int>> array;

public:
    array() {
    }

    array(int m, int n) : array(m, vector<int>(n)) {
    }

    void setArray() {
        for (size_t row = 0; row < array.size(); row++) {
            for (size_t col = 0; col < array[row].size(); col++) {
               std::cin>>array[row][col];
            }
        }
    }
};
#包括
#包括
类数组{
私人:
std::矢量阵列;
公众:
数组(){
}
数组(int m,int n):数组(m,向量(n)){
}
void setArray(){
对于(size_t row=0;row>数组[行][col];
}
}
}
};

使用向量意味着您不必自己进行动态内存分配,从而生成更安全、更可读的代码。

首先,您不应该将数组命名为“array”,它是一个保留的类名。其次,在为它们声明值之前,您试图使用'm'和'n'。您应该首先为它们声明值。希望这会有所帮助。

如果m和n是变量,在大多数情况下,您无法写入

int foo[m][n];  
<> p>除非m和n是代码> const 编译时已知的变量,或者使用GNU C++,它有这样的扩展,或者它们是模板参数(也有它自己的问题,和解决方法)。 进一步阅读:


我在这里假设数组维度在编译时是未知的。John描述的“向量向量”技术基本上是有效的。但是,缺点是需要分配许多单独的内存块,每个基本向量一个

例如,如果二维整数数组是一个1920x1080位图图像,每个像素有一个整数值,这意味着我们将在对象创建时调用malloc 1080次,并且在对象发布时调用free/delete 1080次。这可能会非常昂贵。此外,现有的图形库将坚持每个图像使用一个存储块

为了解决这些问题,我们可以使用一个单独的、私有的一维标准向量,并安排访问函数,以便表达式mat[i][j]仍能按预期工作。最简单的方法是将mat[i]重载,使其返回行开头的地址。这样,mat[i]指针值自然可以用[j]重新编制索引。生成的数组访问不会进行范围检查,就像普通std::vector对象一样

该想法在下面的代码示例中实现:

#include  <vector>
#include  <iostream>
#include  <fstream>


class array2d {
private:
    size_t  rowCount, colCount;
    std::vector<int>  vec;

public:
    array2d(size_t m, size_t n) : rowCount(m), colCount(n)
    {
        size_t  size  = m*n;
        vec           = std::vector<int>(size, 0);

        int*  basePtr = vec.data();

        for (size_t i=0; i < size; i++)
            basePtr[i] = 0;
    }

    inline size_t getRowCount() { return rowCount; }

    inline size_t getColCount() { return colCount; }


    inline int* operator[](size_t rowId)  // HERE !
    {
         // NOT range-checked just like std::vector
         return (vec.data() + (rowId*(this->colCount)));
    }


    void setArrayFromFile(std::istream& file) {
        for (size_t row = 0; row < rowCount; row++) {
            int*  rowStart = (*this)[row];
            for (size_t col = 0; col < colCount; col++) {
                file >> rowStart[col];
            }
        }
    }

    // specialized version to read from standard input:
    void setArray()
    {
        setArrayFromFile(std::cin);
    }
};

//---------------------------------------------------------

  // EXAMPLE OF USE:


void printIntegerMatrix(array2d& matrix)
{
    size_t  rowCount = matrix.getRowCount();
    size_t  colCount = matrix.getColCount();

    for (size_t row = 0; row < rowCount; row++) {
        for (size_t col = 0; col < colCount; col++) {
            std::cout << matrix[row][col] << " ";
        }
        std::cout << std::endl;
    }
}


void processIntegerMatrix(array2d& matrix, const std::string& fileName)
{
    // fill in matrix from a text file then print its contents on stdout:

    std::ifstream  inputStream{fileName};
    if (!inputStream) {
        std::cerr << " Cannot open file " << fileName << std::endl;
        exit(EXIT_FAILURE);
    }

    matrix.setArrayFromFile(inputStream);
    inputStream.close();

    std::cerr << "After reading from file." << std::endl;

    printIntegerMatrix(matrix);
}


int main(int argc, const char* argv[])
{
    size_t  rowCount = 3;
    size_t  colCount = 6;

    // have a text file with some numbers in it:

    std::string    fileName{"numbers1.dat"};

    std::cout << "matrix size: " << rowCount << 'x' << colCount << std::endl;

    // create zero-filled matrix:
    array2d  mat1(rowCount, colCount);

    processIntegerMatrix(mat1, fileName);

    return EXIT_SUCCESS;
}
#包括
#包括
#包括
类数组2d{
私人:
大小\u t行数、列数;
std::vec;
公众:
数组2d(大小为m,大小为n):行数(m),列数(n)
{
尺寸=m*n;
vec=std::vector(大小,0);
int*basePtr=vec.data();
对于(大小i=0;icolCount));
}
void setArrayFromFile(std::istream&file){
对于(大小\u t行=0;行<行计数;行++){
int*rowStart=(*this)[row];
用于(大小\u t col=0;col>行开始[col];
}
}
}
//从标准输入读取的专用版本:
void setArray()
{
setArrayFromFile(std::cin);
}
};
//---------------------------------------------------------
//使用示例:
无效打印整数矩阵(阵列2D和矩阵)
{
size_t rowCount=matrix.getRowCount();
size\u t colCount=matrix.getColCount();
对于(大小\u t行=0;行<行计数;行++){
用于(大小\u t col=0;colSTD::我想用户可以输入数组的大小。你有不协调吗?我想问你更多的细节。@ KaiJI认为你应该只阅读C++中的动态内存分配。然后,网上有很多好的教程。@ JavyTeePAPAT使用向量向量。数组是刚性的和不灵活的。ate构造函数中的内存(新int[m][n])。您也可以使用std::vector.array不是保留名称,它是std中已使用的名称
#include  <vector>
#include  <iostream>
#include  <fstream>


class array2d {
private:
    size_t  rowCount, colCount;
    std::vector<int>  vec;

public:
    array2d(size_t m, size_t n) : rowCount(m), colCount(n)
    {
        size_t  size  = m*n;
        vec           = std::vector<int>(size, 0);

        int*  basePtr = vec.data();

        for (size_t i=0; i < size; i++)
            basePtr[i] = 0;
    }

    inline size_t getRowCount() { return rowCount; }

    inline size_t getColCount() { return colCount; }


    inline int* operator[](size_t rowId)  // HERE !
    {
         // NOT range-checked just like std::vector
         return (vec.data() + (rowId*(this->colCount)));
    }


    void setArrayFromFile(std::istream& file) {
        for (size_t row = 0; row < rowCount; row++) {
            int*  rowStart = (*this)[row];
            for (size_t col = 0; col < colCount; col++) {
                file >> rowStart[col];
            }
        }
    }

    // specialized version to read from standard input:
    void setArray()
    {
        setArrayFromFile(std::cin);
    }
};

//---------------------------------------------------------

  // EXAMPLE OF USE:


void printIntegerMatrix(array2d& matrix)
{
    size_t  rowCount = matrix.getRowCount();
    size_t  colCount = matrix.getColCount();

    for (size_t row = 0; row < rowCount; row++) {
        for (size_t col = 0; col < colCount; col++) {
            std::cout << matrix[row][col] << " ";
        }
        std::cout << std::endl;
    }
}


void processIntegerMatrix(array2d& matrix, const std::string& fileName)
{
    // fill in matrix from a text file then print its contents on stdout:

    std::ifstream  inputStream{fileName};
    if (!inputStream) {
        std::cerr << " Cannot open file " << fileName << std::endl;
        exit(EXIT_FAILURE);
    }

    matrix.setArrayFromFile(inputStream);
    inputStream.close();

    std::cerr << "After reading from file." << std::endl;

    printIntegerMatrix(matrix);
}


int main(int argc, const char* argv[])
{
    size_t  rowCount = 3;
    size_t  colCount = 6;

    // have a text file with some numbers in it:

    std::string    fileName{"numbers1.dat"};

    std::cout << "matrix size: " << rowCount << 'x' << colCount << std::endl;

    // create zero-filled matrix:
    array2d  mat1(rowCount, colCount);

    processIntegerMatrix(mat1, fileName);

    return EXIT_SUCCESS;
}