繁体   English   中英

创建由 C++ 中的参数输入指定大小的矩阵(二维数组)

[英]Create matrix (2d-array) of size specified by parameter input in C++

我正在学习 C++,主要是 Python,R 和 Z9778840A0100CB30C92B78。

arrays (以及与一维数组有某种不同的向量?和二维数组的矩阵?)在 C++ 中的工作方式似乎完全不同,因为我无法使用 ZA384F1AB45.68D178 中的参数指定数组的维度大小

我的目标的一个玩具示例是这样的:

  1. 有一个 function my_2d_array需要两个 arguments MN并返回一个矩阵或二维数组(MxN) ,其中元素指示该元素的 position。 例如调用my_2d_array(4,3)将返回:

    [[00, 01, 02],
    [10, 11, 12],
    [20, 21, 22],
    [30, 31, 32]]

  2. function 应该执行my_2d_array并且能够使用结果执行计算或修改它。

这是我的尝试(有错误):

int my_2d_array(int N, int M) {
    int A[N][M];
    
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            std::string element = std::to_string(i) + std::to_string(j);
            A[i][j] = element;
        }
    }
    return A;
}


void main() {
    int N, M;
    N = 4;
    M = 3;

    int A[N][M] = my_2d_array(N, M);

    // Print the array A
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            std::cout << A[i][j] << " ";
        }
        std::cout << "\n";
    }

}

@JustLearning 建议的一 (1) 个维度尝试:

int my_array(int N) {
    std::array<int, N> A;

    for (int i = 0; i < N; i++) {
        A[i] = i;
    }
    return A;
}


int main() {
    int N = 4;

    int A[N] = my_array(N);

    // Print the array A
    for (int i = 0; i < N; i++) {
        std::cout << A[i] << " ";
    }

}

欢迎使用 C++! 您的 function my_2d_array有几个问题:

  • 返回类型是int ,但是您正试图返回一个int数组。
  • C++ 中的数组标识符实际上是指向该数组第一个元素的指针。 因此,当您返回A时,您必须知道如何将它传递给代码main中的新变量。 特别是,您的代码正在传递对临时变量A的引用,这是不允许的或不安全的。

此外,在 C++ 中,除非您知道自己在做什么,否则main应始终返回一个int

int main() { ... }

您的问题尚不清楚您是尝试实现自己的“阵列”class,还是只是想使用标准中已经建立的 arrays。 对于后者, std::array是一个很好的起点。 优点是您可以从返回intdouble的函数中返回std::array

如果您打算使用固定大小的 arrays ,则std::array很好,因为大小成为类型的一部分: std::array<int, 3> my_array; . 然后您可以手动或使用 class 的成员函数填写它(参见上面链接的 dox)。

如果出于某种原因您更喜欢使用动态大小的 arrays(在运行程序期间会发生变化的大小), std::vector是 go 的方式。

最后,如果您实际上是通过尝试实现容器MyArray来学习 C++ ,您应该在问题中指定这一点,并更具体地说明您需要什么帮助。

这是 1d 中的一个工作示例:

#include <iostream>
#include <array>

template <int N>
std::array<int, N> my_array() {
    std::array<int, N> A;                                                                                                                                                                    
                                                                                                                                                                                             
    for (int i = 0; i < N; i++) {                                                                                                                                                            
        A[i] = i;                                                                                                                                                                            
    }                                                                                                                                                                                        
    return A;                                                                                                                                                                                
}                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                             
int main() {                                                                                                                                                                                 
    const int N = 4;                                                                                                                                                                         
                                                                                                                                                                                             
    std::array<int, N> arr = my_array<N>();                                                                                                                                                  
                                                                                                                                                                                             
    // Print the array A                                                                                                                                                                     
    for (int i = 0; i < N; i++) {                                                                                                                                                            
        std::cout << arr[i] << " ";                                                                                                                                                          
    }                                                                                                                                                                                        
}      

由于std::array的大小包含在它的type中,因此您需要创建一个 function模板,它基本上是适用于不同类型的 function 模板。 (在 C++ 中, std::array<int, 3>std::array<int, 4>被认为是不同的类型。)

为了在main中使用它,索引被提升为const int ,因为普通的int在运行时可能会发生变化,因此不适合定义类型。 (在 C++ 行话中,查找常量表达式)。

最后,请注意,返回类型和接收 function 返回值的变量的类型都必须是std::array ,而不是您在一维代码中尝试的int

您可以像这样使用二维向量

vector<vector int> A;

它的工作方式与二维数组相同

根据您的评论,我可以理解为什么您在尝试在代码中使用矩阵时感到困惑。

C++中有多种容器。 其中许多您可以在标准库中找到( std::vectorstd::liststd::set ,...),其他您可以自己创建或使用其他库。 普通的 arrays (如int a[5] )是一个有点独特的情况,因为它们来自 C 并且是语言本身的一部分。

一个普通数组存在于堆栈上(不是很重要,但您可能想阅读堆栈与堆分配),并引用 memory 的连续区域。

如果你声明一些数组aint a[5] ,你会一个接一个地得到一个 5 个整数的区域,你可以通过写a来指向第一个。 您可以使用a[i]或等价的*(a+i)访问它们中的每一个。

如果你声明a like int a[5][3] ,你现在得到一个 15 个整数的区域,但是你可以稍微不同地访问它们,比如a[i][j] ,它相当于*(a+i*3+j)

对您来说重要的是大小(5 和 3)必须是编译时常量,并且您不能在运行时更改它们。

对于std::array也是如此:您可以声明a std::array<std::array<int, 3, 5> a并获得类似的 15 个整数区域,您可以以相同的方式访问,但是有一些方便(例如,您可以返回该类型,而您不能返回纯数组类型,只能返回一个指针,从而在过程中丢失大小信息)。

我的建议是不要将这些 arrays 视为具有维度,而是将其视为简单的容器,可以为您提供一些 memory 以供您选择使用。 您可以很好地声明a like std::array<int, 15> a并通过如下索引以 2D 方式访问元素: a[i*3+j] 就记忆而言,它是一样的。

现在,如果您希望能够在运行时设置大小,您可以以类似的方式使用std::vector 您要么声明a like std::vector<std::vector<int>> a(5, std::vector<int>(3))并处理嵌套向量(初始化创建 5 std::vector<int>每个大小为 3),或者您a声明为单个向量,例如std::vector<int> a(15)并将其索引为a[i*3+j] 您甚至可以制作自己的 class 包装矢量并帮助索引。

无论哪种方式,在 C++ 中很少需要普通数组,您通常应该使用某种容器,其中std::vector是很多事情的不错选择。

下面是一个使用向量的代码示例:

#include <vector>
#include <string>
#include <iostream>

std::vector<std::string> my_2d_array(int N, int M) {
    std::vector<std::string> A(N*M);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            std::string element = std::to_string(i) + std::to_string(j);
            A[i*M+j] = element;
        }
    }
    return A;
}

int main() {
    int N, M;
    N = 4;
    M = 3;
    std::vector<std::string> A = my_2d_array(N, M);

    // Print the array A
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            std::cout << A[i*M+j] << " ";
        }
        std::cout << "\n";
    }
}

这是一个非常粗略的矩阵 class 示例,用于包装向量:

#include <vector>
#include <string>
#include <iostream>

template<typename T>
class Matrix {
public:
    Matrix(int rowCount, int columnCount) : v(rowCount*columnCount), columnCount(columnCount) {}
    T& operator()(int row, int column) {
        return v[row*columnCount + column];
    }

private:
    std::vector<T> v;
    int columnCount;
};

Matrix<std::string> my_2d_array(int N, int M) {
    Matrix<std::string> A(N, M);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            std::string element = std::to_string(i) + std::to_string(j);
            A(i, j) = element;
        }
    }
    return A;
}

int main() {
    int N, M;
    N = 4;
    M = 3;
    Matrix<std::string> A = my_2d_array(N, M);

    // Print the array A
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            std::cout << A(i, j) << " ";
        }
        std::cout << "\n";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM