可变大小的二维数组作为参数 C++
2D arrays with variable sizes as arguments C++
我打算创建一个函数 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;
}
我该如何处理这个问题?或者我应该只传递两个矩阵作为参数,然后在函数中找到它们的大小?感谢任何帮助。
假设您的二维数组是 "regular" C++ 二维数组(不是向量),请注意数组将其数据存储在连续内存中。
鉴于这一事实,要找到任何二维数组中的最大元素,只需知道维度,然后使用 std::max_element to get a pointer to the maximum element of each array, and call std::swap 交换找到的元素。
#include <algorithm>
#include <iostream>
//...
int main()
{
const int m = 10;
const int n = 20;
int A[m][n] = {0};
int B[m][n] = {0};
// Test
A[4][5] = 20;
B[3][2] = 50;
std::cout << "Before:\n" << A[4][5] << " " << B[3][2];
// Swap the maximum elements
std::swap(*std::max_element(&A[0][0], &A[m-1][n]),
*std::max_element(&B[0][0], &B[m-1][n]));
std::cout << "\n\nAfter:\n" << A[4][5] << " " << B[3][2];
}
输出:
Before:
20 50
After:
50 20
无需创建单独的函数。
如果您必须创建一个函数,那么模板函数可能就是您正在寻找的:
#include <algorithm>
#include <iostream>
//...
template <int M, int N>
void Swap(int A[M][N], int B[M][N])
{
std::swap(*std::max_element(&A[0][0], &A[M-1][N]),
*std::max_element(&B[0][0], &B[M-1][N]));
}
int main()
{
const int m = 10;
const int n = 20;
int A[m][n] = {0};
int B[m][n] = {0};
A[4][5] = 20;
B[3][2] = 50;
std::cout << "Before:\n" << A[4][5] << " " << B[3][2];
Swap<10,20>(A, B);
std::cout << "\n\nAfter:\n" << A[4][5] << " " << B[3][2];
}
输出:
Before:
20 50
After:
50 20
根据您的要求,这里可能会使用向量的向量(也称为 2D 向量)实现您的代码,并附注:
#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
我打算创建一个函数 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;
}
我该如何处理这个问题?或者我应该只传递两个矩阵作为参数,然后在函数中找到它们的大小?感谢任何帮助。
假设您的二维数组是 "regular" C++ 二维数组(不是向量),请注意数组将其数据存储在连续内存中。
鉴于这一事实,要找到任何二维数组中的最大元素,只需知道维度,然后使用 std::max_element to get a pointer to the maximum element of each array, and call std::swap 交换找到的元素。
#include <algorithm>
#include <iostream>
//...
int main()
{
const int m = 10;
const int n = 20;
int A[m][n] = {0};
int B[m][n] = {0};
// Test
A[4][5] = 20;
B[3][2] = 50;
std::cout << "Before:\n" << A[4][5] << " " << B[3][2];
// Swap the maximum elements
std::swap(*std::max_element(&A[0][0], &A[m-1][n]),
*std::max_element(&B[0][0], &B[m-1][n]));
std::cout << "\n\nAfter:\n" << A[4][5] << " " << B[3][2];
}
输出:
Before:
20 50
After:
50 20
无需创建单独的函数。
如果您必须创建一个函数,那么模板函数可能就是您正在寻找的:
#include <algorithm>
#include <iostream>
//...
template <int M, int N>
void Swap(int A[M][N], int B[M][N])
{
std::swap(*std::max_element(&A[0][0], &A[M-1][N]),
*std::max_element(&B[0][0], &B[M-1][N]));
}
int main()
{
const int m = 10;
const int n = 20;
int A[m][n] = {0};
int B[m][n] = {0};
A[4][5] = 20;
B[3][2] = 50;
std::cout << "Before:\n" << A[4][5] << " " << B[3][2];
Swap<10,20>(A, B);
std::cout << "\n\nAfter:\n" << A[4][5] << " " << B[3][2];
}
输出:
Before:
20 50
After:
50 20
根据您的要求,这里可能会使用向量的向量(也称为 2D 向量)实现您的代码,并附注:
#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