You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将C++代码中三个功能相似的数组计算函数合并为一个通用函数?

C++ 多维度数组通用统计函数实现方案

实现思路

你现有三个函数的运算逻辑完全相同,唯一差异是硬编码了全局常量k/b/u作为数组遍历边界,只需要给通用函数新增行数、列数两个入参替换硬编码值,即可适配任意尺寸的二维float数组。

优化后完整可运行代码

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;

// 通用统计函数,入参分别为:二维数组指针、数组行数、数组列数
void calc(float** array, int rows, int cols);

int main()
{
    // 全局只初始化一次随机种子即可
    srand(time(0));
    
    // 第一个3*3数组
    float** array = new float* [m];
    for (int l = 0; l < m; l++) {
        array[l] = new float[k];
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < k; j++) {
            array[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < k; j++) {
            cout << setprecision(2) << setw(4) << array[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The statistics of each column: " << endl;
    calc(array, m, k);
    // 释放内存
    for (int l = 0; l < m; l++) delete[] array[l];
    delete[] array;

    // 第二个4*4数组
    float** arr = new float* [b];
    for (int l = 0; l < b; l++) {
        arr[l] = new float[b];
    }
    for (int i = 0; i < b; i++) {
        for (int j = 0; j < b; j++) {
            arr[i][j] = rand() % 21 - 10;
        }
    }
    cout << endl << "The initial array is: " << endl << endl;
    for (int i = 0; i < b; i++)
    {
        for (int j = 0; j < b; j++) {
            cout << setprecision(2) << setw(4) << arr[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The statistics of each column: " << endl;
    calc(arr, b, b);
    // 释放内存
    for (int l = 0; l < b; l++) delete[] arr[l];
    delete[] arr;

    // 第三个5*5数组
    float** ar = new float* [u];
    for (int l = 0; l < u; l++) {
        ar[l] = new float[u];
    }
    for (int i = 0; i < u; i++) {
        for (int j = 0; j < u; j++) {
            ar[i][j] = rand() % 21 - 10;
        }
    }
    cout << endl << "The initial array is: " << endl << endl;
    for (int i = 0; i < u; i++)
    {
        for (int j = 0; j < u; j++) {
            cout << setprecision(2) << setw(4) << ar[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The statistics of each column: " << endl;
    calc(ar, u, u);
    // 释放内存
    for (int l = 0; l < u; l++) delete[] ar[l];
    delete[] ar;

    return 0;
}

void calc(float** array, int rows, int cols) {
    for (int j = 0; j < cols; j++)
    {
        // 最大值类型改为float和数组元素匹配,统计负数的n改为局部变量
        float max = array[0][j];
        int negative_cnt = 0;
        for (int i = 0; i < rows; i++) {
            if (array[i][j] > max)
                max = array[i][j];
            if (array[i][j] < 0) {
                negative_cnt += 1;
            }
        }
        cout << endl << "IN the [" << j + 1 << "] column is " << negative_cnt << " negative elements" << endl << endl;
        cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
    }
}

核心修改点

  • 新增行数、列数两个入参到通用统计函数,替换原有硬编码的全局维度常量,适配任意尺寸的二维float数组
  • 修复原有逻辑bug:将最大值变量类型从int改为float,避免浮点型数值被截断
  • 移除冗余全局变量,改为局部变量,避免全局变量污染
  • 移除重复的随机种子初始化调用,仅在程序启动时初始化一次
  • 补充动态内存释放逻辑,避免内存泄漏

内容的提问来源于stack exchange,提问作者Paw Max1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 23:09:03