【问题标题】:Static array and Dynamic array in a function函数中的静态数组和动态数组
【发布时间】:2023-02-10 18:29:22
【问题描述】:

我有一个问题如下 C++ 代码。

这是可以成功执行的代码(无功能)。

#include <iostream>
using namespace std;


int main()
{
    int size_3 = 3;
    //arr1 is a static array
    int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

    //arr2 is a dynamic array
    int** arr2 = new int* [size_3];
    for (int i = 0; i < size_3; i++) {
        arr2[i] = new int[size_3];
    }

    int val = 9;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            arr2[i][j] = val--;
        }
    }

    cout << "Elements in matrix 1: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr1[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << "Elements in matrix 2: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr2[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

    int** prod_arr = new int* [size_3];
    for (int i = 0; i < size_3; i++) {
        prod_arr[i] = new int[size_3];
    }


    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            prod_arr[i][j] = 0;
            for (int k = 0; k < size_3; k++) {
                prod_arr[i][j] += arr1[i][k] * arr2[k][j];
            }
        }
    }

    cout << "Elements in the product of 2 matrices: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << prod_arr[i][j] << " ";
        }
        cout << endl;
    }

}

但是,我将使用该函数对 2 个矩阵进行乘法运算。 我怎么能移动它来运行,因为第一个数组是静态数组,第二个数组是动态数组。

我尝试了不同的方法来使用指针,在函数中传递引用,但仍然无法工作。

我的无效代码如下。

#include <iostream>
using namespace std;

int** function(int farr1, int farr2, int size3) {

    int** prod_arr = new int* [size3];
    for (int i = 0; i < size3; i++) {
        prod_arr[i] = new int[size3];
    }

    for (int i = 0; i < size3; i++) {
        for (int j = 0; j < size3; j++) {
            prod_arr[i][j] = 0;
            for (int k = 0; k < size3; k++) {
                prod_arr[i][j] += farr1[i][k] * farr2[k][j];
            }
            return prod_arr[i][j];
        }
    }
}


int main()
{
    int size_3 = 3;
    //arr1 is a static array
    int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

    //arr2 is a dynamic array
    int** arr2 = new int* [size_3];
    for (int i = 0; i < size_3; i++) {
        arr2[i] = new int[size_3];
    }

    int val = 9;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            arr2[i][j] = val--;
        }
    }

    cout << "Elements in matrix 1: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr1[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << "Elements in matrix 2: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << arr2[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    int** prod_arr = function(farr1, farr2, size_q3);
    cout << "Elements in the product of 2 matrices: " << endl;
    for (int i = 0; i < size_3; i++) {
        for (int j = 0; j < size_3; j++) {
            cout << prod_arr[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

我想使用函数来执行代码。 我有 2 个数组,一个是静态的,另一个是动态的, 如何使用指针并传递给这个不同数组的函数。

非常感谢你的帮助。

【问题讨论】:

  • 使用 std::array 和 std::vector,忘记所有这些旧式“C”的东西。动态分配的数组:std::vector<int> values(8);将分配 8 个整数,您甚至不必记得调用 delete[]。接受数组的函数:void f(const std::vector&lt;int&gt;&amp; values)... 奖金值将知道它的大小,您可以轻松地在基于范围的 for 循环中使用它。返回数组:std::vector&lt;int&gt; get_values()
  • 将其传递给函数时,无论是静态还是动态分配的数组都没有区别。
  • 在函数function 中,您何时何地从函数中return?请记住 return 语句返回立即地.一些快速的 rubber duck debugging 会有所帮助。
  • 你说你想把它传递给函数,但函数只有int参数
  • 看起来您正在从过时的来源学习 C++。学习 cpp 的好来源是:cppreference。一个 recent C++ book 或去 learncpp.com 试试(这很不错,而且是最新的)。当您从这些资源中掌握了 C++ 基础知识后,请定期查看 C++ coreguidelines 以保持最新。

标签: c++ arrays dynamic-arrays


【解决方案1】:

一些帮助您入门的 C++ 数组示例: 二维动态数组可以建模为std::vector&lt;std::vector&lt;int&gt;&gt;

#include <array>        // static array
#include <vector>       // dynamic array, can resize at runtime
#include <iostream>

// https://en.cppreference.com/w/cpp/container/array
// accept a modifiable static array (content can be changed
// the & means by reference, the array will not get copied
// https://isocpp.org/wiki/faq/references
void func1(std::array<int, 4>& values)  
{
    values[2] = 3;
}

// array cannot be modified in body of func2 only used : const
void func2(const std::array<int, 4>& values)
{
    std::cout << values[2] << "
";
}

// https://en.cppreference.com/w/cpp/container/vector
// return a dynamically allocated array of integers
// it will have a size of 5 when returned
std::vector<int> get_values()
{
    return std::vector<int>{1, 2, 3, 4, 5};
}


int main()
{
    auto values = get_values();
    values.push_back(6); // add another value

    // https://en.cppreference.com/w/cpp/language/range-for
    // prefer to use those if you don't need indices 
    // (which is not as often as you think)
    for (const int value : values)
    {
        std::cout << value << " ";
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 1970-01-01
    • 2011-02-09
    • 2020-09-18
    • 2012-03-22
    • 1970-01-01
    • 2012-05-09
    • 2018-09-03
    • 2021-02-08
    相关资源
    最近更新 更多