c++ - 以可变大小作为参数的二维数组 C++

标签 c++ multidimensional-array swap

我要创建一个函数Swap()交换n×m矩阵A和矩阵B的两个最大元素。 但我有error: declaration of 'A' as multidimensional array must have bounds for all dimensions except the first

void Swap(int A[][], int B[][], int n, int m)
{
    int max_A = A[0][0];
    int max_B = B[0][0];
    int index_Ai, index_Aj, index_Bi, index_Bj;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(max_A < A[i][j])
            {
                max_A = A[i][j];
                index_Ai = i;
                index_Aj = j;
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(max_B < B[i][j])
            {
                max_B = B[i][j];
                index_Bi = i;
                index_Bj = j;
            }
        }
    }
    int temp;
     temp = A[index_Ai][index_Aj];
     A[index_Ai][index_Aj] = B[index_Bi][index_Bj];
     B[index_Bi][index_Bj] = temp;
}

我该如何处理这个问题?或者我应该只传递两个矩阵作为参数,然后在函数内找到它们的大小?如有任何帮助,我们将不胜感激。

最佳答案

按照您的要求,这里是使用 vector 的 vector (又称 2D vector )可能实现您的代码,并带有注释:

Live demo

#include <iostream>
#include <vector>
#include <exception>

//pass vectors by reference so the changes are reflected in the passed arguments
void Swap(std::vector<std::vector<int>>& A, std::vector<std::vector<int>>& B) {

    int max_A = A.at(0).at(0); //if indexing A[0][0], exception would not be thrown
    int max_B = B.at(0).at(0);
    int index_Ai, index_Aj, index_Bi, index_Bj;

    for (size_t i = 0; i < A.size(); i++) {
        for (size_t j = 0; j < A.at(0).size(); j++) {
            if (max_A < A[i][j]) { //indexing A[i][j] safe, cycle limited to vector size
                max_A = A[i][j];
                index_Ai = i;
                index_Aj = j;
            }
        }
    }
    for (size_t i = 0; i < B.size(); i++) {
        for (size_t j = 0; j < B.at(0).size(); j++) {
            if (max_B < B[i][j]) {
                max_B = B[i][j];
                index_Bi = i;
                index_Bj = j;
            }
        }
    }
    //standard library swap function
    std::swap(A.at(index_Ai).at(index_Aj), B.at(index_Bi).at(index_Bj));
}
int main() {

    std::vector<std::vector<int>> A = {{1, 2, 300}, {4, 9, 10, 56, 5, 6}};
    std::vector<std::vector<int>> B = {{10, 45, 2, 12, 20, 80}, {40, 45, 500, 60}};
    try{
        Swap(A, B);
    } catch(std::exception& e){ //if vectors accessed out of bounds throws exception
        std::cout << "ERROR: " << e.what() << std::endl; //we catch it here
        return EXIT_FAILURE;    
    }

    for (auto &v : A) { //test print A
        for (auto i : v) {
            std::cout << i << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
    for (auto &v : B) { //test print B
        for (auto i : v)
        {
            std::cout << i << " ";
        }
        std::cout << std::endl;
    }
    return EXIT_SUCCESS;
}

初始数组:

1 2 300
4 9 10 56 5 6
10 45 2 12 20 80
40 45 500 60

交换后:

1 2 500
4 9 10 56 5 6 
10 45 2 12 20 80
40 45 300 60

关于c++ - 以可变大小作为参数的二维数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62214908/

相关文章:

matlab - y = A *x + B 的多维线性回归

c++ - 根据标准,这种没有破坏的交换实现是否有效?

azure - 更换azure网站失败

c++ - 找到一组曲线的凸包络的算法或想法是什么?

php - 多维数组上的 json_encode() - 带有字符串键

javascript - 不想在 Javascript 中打印 Array of Array

java - 为什么我的交换方法不起作用?

c++ - Cryptic template 模板参数错误

c++ - 带有转发的延迟初始化

python - Pybind11:将字符串*参数传递给构造函数