【问题标题】:How to delete column in 2d array c++ with dynamic array?如何使用动态数组删除二维数组 C++ 中的列?
【发布时间】:2021-12-30 07:50:14
【问题描述】:

我想删除二维数组中最大整数的列,我是这样做的,但是为什么要删除列和行呢?我可以解决这个问题并只删除列吗?任务是用删除命令来做,但现在我认为这是不可能的

#include <iostream>

using namespace std;

int main()
{

int row = 3, col = 3;
int** arr = new int* [row];
for(int i = 0; i < row; i++){
    arr[i] = new int[col];
}
for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++) {
        cin >> arr[i][j];
    }
}
for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++) {
        cout << arr[i][j] << " ";
    }
    cout << endl;
}
    cout << " ------------- " << endl;

int max = 0, index = 0;
for(int i =0; i < row; i++){
    for(int j = 0; j < col; j++){
        if(arr[i][j] > max){
            max = arr[i][j];
            index = i;
        }
    }
}
delete [] arr[index];
int** tmp = new int*[index - 1];
int tmpI = 0;
for(int i = 0; i < col; i++){
    if(i != index){
        tmp[tmpI++] = arr[i];
    }
}
delete [] arr;
arr = tmp;
col = col - 1;

for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++) {
        cout << arr[i][j] << " ";
    }
    cout << endl;
}

}

【问题讨论】:

  • 我推荐使用std::vector
  • index 是行索引,而不是列索引。但令人费解的是,复制到 tmp 的循环在 i &lt; col 时运行,而不是在 i &lt; row 时运行,就像它在其他任何地方一样。目前还不清楚你想在那里做什么。
  • 另外,int** tmp = new int*[index - 1]; 没有意义。例如。如果index == 0 - 最大值在第一行怎么办?
  • 一维数组在索引上有一些数学是二维数组的最佳表示
  • 您的代码有缺陷。您正在删除一行而不是一列。仅使用 1 个delete[] 语句无法删除二维数组中的列。您的二维数组有 3 行。这些行中的每一行都必须首先复制到一个临时的一维数组,然后删除,然后使用new int[3 - 1] 创建并替换为前一行的指针。然后必须将临时一维数组的内容复制回新创建的行,不包括必须删除的列。

标签: c++ for-loop multidimensional-array dynamic-memory-allocation dynamic-arrays


【解决方案1】:

对于初学者,变量索引设置为行号

index = i;

那么这一行就被删除了

delete [] arr[index];

但是您要删除一列而不是一行。所以这段代码没有意义。

您也错误地搜索了最大元素。如果用户输入所有负值,则最大值将等于 0,尽管数组中不存在具有该值的元素。

在此声明中

int** tmp = new int*[index - 1];

您分配的数组的行数比原始数组中的行数少一。此外,如果 index 等于 0,则分配的内存范围非常大。

此声明

delete [] arr;

产生内存泄漏。

看来你需要的是类似下面的东西

#include <iostream>

int main() 
{
    size_t row = 3, col = 3;
    
    int **arr = new int * [row];

    for ( size_t i = 0; i < row; i++ )
    {
        arr[i] = new int[col];
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ ) 
        {
            std::cin >> arr[i][j];
        }
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ ) 
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    std::cout << "------------- " << std::endl;
    
    size_t max_i = 0, max_j = 0;
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            if ( arr[max_i][max_j] < arr[i][j] )
            {
                max_i = i; max_j = j;
            }
        }
    }
    
    int **tmp = new int*[row];
    for ( size_t i = 0; i < row; i++ )
    {
        tmp[i] = new int[col-1];
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0, k = 0; j < col; j++ )
        {
            if ( j != max_j ) tmp[i][k++] = arr[i][j];
        }
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        delete [] arr[i];
    }
    
    delete [] arr;
    
    arr = tmp;
    
    --col;
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ ) 
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        delete [] arr[i];
    }
    
    delete [] arr;
    
    return 0;
}

程序输出可能看起来像

1 2 3 
6 5 4 
7 9 8 
 ------------- 
1 3 
6 4 
7 8 

【讨论】:

    【解决方案2】:

    这里有一个替代建议:您以row x column 格式存储数据。如果您更改为column x row 格式,则删除列会变得更容易。我还制作了一个辅助函数来打印矩阵,并将输入循环和找到最大值的循环结合起来。

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>  // for rand
    #include <climits>
    
    using namespace std;
    
    void print_matrix(int** arr, int rows, int columns)
    {
        for(int row = 0; row < rows; row++){
            for(int col = 0; col < columns; col++) {
                cout << setw(2) << arr[col][row] << " ";
            }
            cout << "\n";
        }
    }
    
    int main()
    {
        srand(time(nullptr));  // For testing
        
        int rows = 3, columns = 3;
        
        // Create the matrix
        int** arr = new int *[columns];
        for (int col = 0; col < columns; col++) {
            arr[col] = new int[rows];
        }
        
        // Input values - finding max, too
        int max_value = INT_MIN, max_value_column = -1;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < columns; col++) {
                //cin >> arr[col][row];
                arr[col][row] = rand() % 50;    // Using rand for testing.
                if (arr[col][row] > max_value) {
                    max_value = arr[col][row];
                    max_value_column = col;
                }
            }
        }   
        print_matrix(arr, rows, columns);
        cout << " ------------- " << endl;
    
        // Delete the column with max
        delete [] arr[max_value_column];
        columns--;
        // Shift columns to the right of the deleted column left one
        for (int col = max_value_column; col < columns; col++) {
            arr[col] = arr[col + 1];
        }
        print_matrix(arr, rows, columns);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2016-10-15
      • 2014-12-05
      • 2011-04-11
      • 2011-01-11
      • 2020-06-19
      相关资源
      最近更新 更多